我正在本地PC上运行IIS中的一个网站,我将一个日期保存为UTC,并在前端mvc网站上调用.ToLocalTime()。如果我在我的本地PC上运行该网站,它会输出正确的日期和时间,以适应DST。如果我在英国西部地区的Azure运行它,我会落后1小时。这直到今天才成为一个问题,也就是星期六的钟变了。我需要和Azure一起开罚单吗?还是我做错了?
发布于 2017-03-29 05:19:54
Azure应用服务器正在使用UTC时间。如果你的网站不是全球性的,只适用于英国。我们可以使用以下代码获取英国本地时间。
DateTime utcNow = DateTime.UtcNow;
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"); //UK time zone
DateTimeOffset time = TimeZoneInfo.ConvertTimeFromUtc(utcNow, tz);
lab_time.Text = time.DateTime.ToString(CultureInfo.InvariantCulture); //display the datetime with label.如果您的网站是全球性的,您可能需要使用javascrpt来做到这一点,更多细节请参阅另一个SO Thread。
发布于 2017-03-27 08:49:34
时区实际上取决于服务器位置。
我也面临过同样的问题。我有我的服务器在新加坡,它过去经常与我的区域,印度。
它也会影响时间和日期格式.
所以,Better not use ToLocalTime() at the server side。只要get the UTC time from the server和convert to,不管是什么local time at the client侧。问题解决了!
希望这能有所帮助。谢谢。
https://stackoverflow.com/questions/43041301
复制相似问题