c# - 在 Azure Web Apps 上更改时区不适用于 DateTimeOffset.Now?

标签 c# asp.net datetime azure azure-web-app-service

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 是怎么回事?有什么办法可以解决这个问题吗?

需要澄清的是,我真的不想更改服务器上的时区。我们正在研究一个适当的独立于时区的解决方案。但我还是很好奇为什么会这样。

最佳答案

对我来说,现在就可以了。如果它不起作用,请确保设置正确的值

  1. 打开注册表
  2. 搜索“时区”
  3. 检查您的区域是如何写入的。例如,我的区域是“东部东部标准时间 (+2)”,在注册表中其名称是“东欧标准时间”

因此,我在 azure 应用程序设置中添加了“WEBSITE_TIME_ZONE:东欧标准时间”,并且它有效。

关于c# - 在 Azure Web Apps 上更改时区不适用于 DateTimeOffset.Now?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30939990/

相关文章:

.net - 从字符串到类型 'Date' 的转换无效

c# - 无法从 List<DateTime?> 转换为 List<dynamic>

c# - 遍历 sql(SQL 或 Asp.net C#)的最佳方法是什么?

c# - 插件架构和共享接口(interface)问题

c# - 使用 ToList() 和 .AsQueryable() 连接两个不同的数据库上下文有什么区别?

c# - AuthenticationTypes.SecureSocketsLayer 和 AuthenticationTypes.Secure 之间有什么区别?

c# - 简单的 ASP.NET 数据库查询

asp.net - 依赖注入(inject)和 ASP.net MVC 5/Web API 2

asp.net - 如何让 ASP.NET WebForms 路由正确路由 .asmx JSON 调用?

javascript - jQuery 根据元素中包含的日期删除元素