问题描述
在蔚蓝的网站DateTime.Now总是给人回UTC,在那里我需要能够设置时区为GMT时间,所以任何时候都目前一个小时了。
In azure websites the DateTime.Now always gives back the time in UTC, where I need to be able to set the timezone to GMT, so all times are currently an hour out.
我的不可以能够改变任何code在这一点,所以问题是,是否默认的时区可以在配置或编程改变(这是可能的设置在Global.asax中默认的文化,以确保日期格式是正确的)。
I'm not able to change any of the code at this point, so the question is whether the default timezone can be changed either in configuration or programatically (it's possible to set the default culture in Global.asax to ensure date formats are correct).
推荐答案
首先,GMT在本质上是一样的UTC - 的 GMT。你可能与BST是GMT + 1混淆吧:00在夏季
First, GMT is essentially the same as UTC - it has no offset. To be precise, UTC supersedes GMT . You are probably confusing it with BST which is GMT+1:00 during summer time.
在一般情况下,这是一个坏主意,使有关时区的假设,特别是在Web应用程序。相反,日期时间,你应该使用的DateTimeOffset。
In general, it's a bad idea to make assumptions about timezones, especially in web applications. Instead of DateTime you should use DateTimeOffset.
幸运的是,是Azure网站最近的变化,现在你的可以的改变支持和可靠的方式蔚蓝网站的时区。从MSDN博客文章
Luckily, there was a recent change for Azure Web Sites and now you can change the timezone of an Azure Web site in a supported and reliable way. From the MSDN blog post
所有你需要做的就是添加一个(通过门户网站或管理API)呼吁WEBSITE_TIME_ZONE,并设置为在Windows注册表下HKLM \\ SOFTWARE \\微软\\的Windows NT \\ CURRENTVERSION \\ Time区域\\定义(例如,时区的名字。澳大利亚东部标准时间)。
不过,Windows拌和时区,并使用名为GMT标准时间是指BST和UTC来称呼GMT / UTC。
Unfortunately, Windows mixes up the timezones and uses the name "GMT Standard Time" to refer to BST and "UTC" to refer to GMT/UTC.
这篇关于C#DateTime.Now在Azure中未报告GMT - 可以将默认时区设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!