C# DateTime.Now in Azure not reporting GMT - can the default timezone be set?(C# DateTime.Now 在 Azure 中不报告 GMT - 可以设置默认时区吗?)
问题描述
在 azure 网站中,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.
我现在无法更改任何代码,所以问题是是否可以通过配置或编程方式更改默认时区(可以在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 相同 - 它没有偏移量.准确地说,UTC 取代 GMT.您可能会将它与夏令时 GMT+1:00 的 BST 混淆.
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 而不是 DateTime.
In general, it's a bad idea to make assumptions about timezones, especially in web applications. Instead of DateTime you should use DateTimeOffset.
幸运的是,Azure 网站最近发生了变化,现在您可以 以受支持且可靠的方式更改 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 并将其设置为在 HKLMSoftwareMicrosoftWindows NtCurrentVersionTime Zones 下的 Windows 注册表中定义的时区名称(例如,澳大利亚东部标准时间").
All you need to do is add an Application Setting (via the portal or the management APIs) called WEBSITE_TIME_ZONE and set that to the name of the time zone as defined in the Windows Registry under HKLMSoftwareMicrosoftWindows NtCurrentVersionTime Zones (for example, "AUS Eastern Standard 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 - 可以设置默认时区吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# DateTime.Now 在 Azure 中不报告 GMT - 可以设置默认时区吗?


- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01