According to multiple postings ,Microsoft 启用了使用应用程序设置 - WEBSITE_TIME_ZONE
- 来控制 Web 服务器时区的功能。
为了尝试此操作,我将此值设置为“东部标准时间”,这是我的本地时区。
在 ASP.NET MVC Razor 页面上,我添加了以下代码:
DateTime.Now: @DateTime.Now
DateTimeOffset.Now: @DateTimeOffset.Now
DateTime.UtcNow: @DateTimeOffset.UtcNow
当我昨晚在东部标准时间下午 5:10:07 运行此命令时,它给出了以下输出:
DateTime.Now: 6/18/2015 5:10:07 PM
DateTimeOffset.Now: 6/18/2015 5:10:07 PM +00:00
DateTime.UtcNow: 6/18/2015 9:10:07 PM
如您所见,该设置正确地允许 DateTime.Now
返回我的时区中的正确值,而不是像 Azure 网站/Web 应用程序通常那样返回 UTC。由于显而易见的原因,DateTime.UtcNow
始终返回正确的值。
但是,DateTimeOffset.Now
返回本地时间,但偏移量为 +00:00
- 几乎就像更改了时钟而不是时区。即使 documentation 也会发生这种情况说(强调我的):
Gets a DateTimeOffset object that is set to the current date and time on the current computer, with the offset set to the local time's offset from Coordinated Universal Time (UTC).
那么,WEBSITE_TIME_ZONE
设置影响 DateTime.Now
但不影响 DateTimeOffset.Now
是怎么回事?有什么办法可以解决这个问题吗?
需要澄清的是,我真的不想更改服务器上的时区。我们正在研究一个适当的独立于时区的解决方案。但我还是很好奇为什么会这样。
最佳答案
对我来说,现在就可以了。如果它不起作用,请确保设置正确的值
- 打开注册表
- 搜索“时区”
- 检查您的区域是如何写入的。例如,我的区域是“东部东部标准时间 (+2)”,在注册表中其名称是“东欧标准时间”
因此,我在 azure 应用程序设置中添加了“WEBSITE_TIME_ZONE:东欧标准时间”
,并且它有效。
关于c# - 在 Azure Web Apps 上更改时区不适用于 DateTimeOffset.Now?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30939990/