将 utc 转换为本地日期时间
我在 web.config 中有一个全球化设置,如下所示:
<globalization culture="de-DE" uiCulture="de-DE" />
在平均 aspx 页面上我输出某种内容。例如:
var a = DateTime.UtcNow.ToLocalTime().ToString();
var b = DateTime.UtcNow.ToString();
var c = DateTime.Now.ToLocalTime().ToString();
var d = TimeZone.CurrentTimeZone.ToLocalTime(DateTime.UtcNow).ToString();
结果:a、b、c、d 的值全部相同(例如 01.01.2001 17:00:00 ),而我希望本地时间和 UTC 时间不同。
我在这里缺少什么以及如何从 UTC 日期获取正确的当地时间..我已经检查了其他主题,但它不起作用..
i've got an globalization setting in the web.config like this:
<globalization culture="de-DE" uiCulture="de-DE" />
and on an average aspx page i output sth. like:
var a = DateTime.UtcNow.ToLocalTime().ToString();
var b = DateTime.UtcNow.ToString();
var c = DateTime.Now.ToLocalTime().ToString();
var d = TimeZone.CurrentTimeZone.ToLocalTime(DateTime.UtcNow).ToString();
Result: The values of a,b,c,d are ALL identical ( e.g. 01.01.2001 17:00:00 ), when i would expect the local and UTC times to be different.
What am i missing here and how can i get the correct localtime from a UTC date.. i checked other topics already, but it didn't work..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无论数据中心位于何处,Microsoft Azure 都将 UTC 时间设置为本地时间。如果您想转换为用户浏览器的本地时间,则需要以编程方式进行转换。
由于 Azure 是一项全球服务,因此本身并不存在本地时间,为了在所有全球数据中心之间保持一致,使用标准时间是有意义的。
更多信息请访问。
http://michaelcollier.wordpress.com/ 2010/05/22/嘿-蔚蓝-现在几点了/
Microsoft Azure is set to have UTC time be the local time no matter where the data center is located. If you want to convert to a time that is local to a user's browser you will need to convert it programatically.
Since Azure is a global service, there really is no local time per se, and to be consistent across all their global data centers, it makes sense to use a standard time.
More information at.
http://michaelcollier.wordpress.com/2010/05/22/hey-azure-what-time-is-it/
正如@John 所指出的,你必须在客户端中执行此操作。如果它是一个网络应用程序,您可以通过以下方式将 UTC 转换为本地时间:
损坏的链接
As @John has pointed out you have to do it in the client. If it's a web application you can convert UTC to local time in this way:
broken link
这就是我发现对我有用的方法:
尽管现在是西欧标准时间,但它仍然考虑到夏令时。 (我让它与 GMT/BST 配合得很好)
我曾希望 ToLocalTime 在转换时能够考虑到 CultureUI/Culture 全球化设置,但情况似乎并非如此。
This is what I've found works for me:
And even though it's W. Europe Standard Time, it still takes into account Summer Time. (I have it working with GMT/BST quite nicely)
I had hoped that ToLocalTime would take into account the CultureUI/Culture globalization settings when converting, but that doesn't seem to be the case.