C# DateTime.Now 在 Azure 中不报告 GMT - 可以设置默认时区吗?

C# DateTime.Now in Azure not reporting GMT - can the default timezone be set?(C# DateTime.Now 在 Azure 中不报告 GMT - 可以设置默认时区吗?)
本文介绍了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 - 可以设置默认时区吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)