在 ASP.NET 中,我应该如何与 GMT 进行转换?
在代码隐藏中:
我使用 DateTime.ToUniversalTime()
将日期存储在 SQL Server 中。
然后,我读取日期,并使用 DateTime.ToLocalTime()
转换回来,然后将其呈现在网页上。
当我的 Web 服务器与客户端位于完全不同的时区时,这不起作用。我的所有转换都在 Web 服务器上进行。在代码隐藏中尝试过此操作后,我相信我需要在客户端中执行此操作。
确保浏览器的实际本地时间与 GMT 正确转换的首选方法是什么?
In Code-behind:
I store my Dates in SQL Server with DateTime.ToUniversalTime()
.
I then read the Date, and convert back with DateTime.ToLocalTime()
before presenting it on the Web page.
When my Web server is in an entirely different time zone from the client this doesn't work. All my conversions are taking place on the Web server. Having tried this in the code-behind, I believe I need to do it in the client.
What is the preferred method for making sure that the actual local time of the browser gets correctly converted to and from GMT?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种选择是在用户的一般配置文件信息中询问他们的时区,然后在服务器上使用该时区。
One option is to ask the user about their timezone in their general profile information, then use that on the server.
如果您不能只显示确定的服务器时区的时间(例如“中午 12:00 中部时间”)或让用户在其个人资料中提供他们的时区,那么您将需要使用 javascript 来转换时间。我不想完成所有步骤,但此页面有一个很好的演练:
https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329 .html
请注意,显示用户本地时间的首选方法是让他们在个人资料中提供时区,以便您可以在服务器上进行此转换。
If you can't just display the time with your server time zone identified (Such as saying "12:00pm Central time") or have the user provide their timezone in their profile, then you will need to use javascript to convert the time. I don't want to go through all of the steps, but this page has a good walkthrough of doing this:
https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329.html
Note that the preferred method of showing users' local time would be to have them provide their timezone in their profile so you can do this conversion on the server.
我见过的大多数需要本地时间的实现都依赖于用户指定他们的时区,然后将其存储在服务器上的配置文件中,然后用于调整显示的日期/时间。这似乎是最稳健的方法。另一种方法是尝试使用 IP 地址(GeoIP 或类似地址)确定用户位置并进行相应调整。这并不完全可靠,因为某些公司的网关未与用户进行地理定位。
Most implementations I've seen where local time is needed rely on the user specifying their timezone, this is then stored in their profile on the server and then used to adjust the presented date/times. This would seem to be the most robust method. An alternative would be to try to establish the users location using their IP address (GeoIP or similar) and adjust accordingly. This isn't wholly reliable as some companies have gateways that aren't geo-located with the user.
google 上的大多数博客都指出 .NET 没有提供获取客户端时区的方法。
然而大多数人说:
要求用户输入他的时区,然后使用该信息来显示数据
使用 javascript函数返回浏览器的时区偏移(以分钟为单位),然后将其发送到服务器进行进一步处理。请参阅:http:// /weblogs.asp.net/cprieto/archive/2010/01/03/handling-timezone-information-in-asp-net.aspx
Most blogs on google point out to the fact that .NET does not provide a way to get the client's time zone.
However most say that either:
Ask the user to enter his timezone and then use that information to display the data
Use javascript function which returns browser’s time zone offset in minutes and then send it across to the server for further processing. Refer: http://weblogs.asp.net/cprieto/archive/2010/01/03/handling-timezone-information-in-asp-net.aspx