[英]Azure web app timezone error in asp.net webapi
I have web app in azure paas environment.我在 azure paas 环境中有 web 应用程序。 I need to convert the time in different timezone, i have following code which run perfectly fine on dev machine but when i deploy on azure paas environment it throw error我需要转换不同时区的时间,我有以下代码在开发机器上运行得很好但是当我在 azure paas 环境上部署时它会抛出错误
TimeZoneInfo serverTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(TimeZone.CurrentTimeZone.StandardName);
return TimeZoneInfo.ConvertTimeToUtc(_lastUpdatedDate.Value, serverTimeZoneInfo);
First line throw exception.第一行抛出异常。 Error getting value from 'DateCreated' on 'ViewModels.Orders.OrderActivityViewModel'从“ViewModels.Orders.OrderActivityViewModel”上的“DateCreated”获取值时出错
A few things:一些东西:
TimeZone
class.不要使用TimeZone
。 It is obsolete.它已经过时了。 Use only the TimeZoneInfo
class.仅使用TimeZoneInfo
class。StandardName
is NOT the same as the Id
for all cases.对于所有情况, StandardName
都与Id
不同。 It is also impacted by OS language, where the Id
is not.它还受操作系统语言的影响,而Id
则不受此影响。TimeZoneInfo.Local.Id
, though there's no point in finding it by Id when you already have it.本地时区的 Id 位于TimeZoneInfo.Local.Id
,尽管当您已经拥有它时,通过 Id 找到它是没有意义的。 You'd just use TimeZoneInfo.Local
.您只需使用TimeZoneInfo.Local
。.ToUniversalTime()
on your original DateTime
or DateTimeOffset
value.如果您只是将本地时间转换为 UTC,则根本不需要弄乱时区,只需在原始DateTime
或DateTimeOffset
值上使用.ToUniversalTime()
。WEBSITE_TIME_ZONE
setting (on Windows only), but I don't recommend that.如有必要,您可以更改 Azure 应用服务的WEBSITE_TIME_ZONE
设置(仅在 Windows 上),但我不建议这样做。 If you have a specific time zone in mind, then you would pass that ID into TimeZoneInfo.FindSystemTimeZoneById
.如果您有特定的时区,则可以将该 ID 传递给TimeZoneInfo.FindSystemTimeZoneById
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.