ToLocalTime显示错误的时间

时间:2017-03-27 08:23:32

标签: asp.net-mvc azure-web-sites

我在我的本地PC上运行IIS中的网站,我将日期保存为UTC并在前端mvc网站上调用.ToLocalTime()。如果我在本地PC上运行该网站,它会输出为夏令时调整的正确日期和时间。如果我在英国西部地区的Azure中运行它,我会落后1小时。这直到今天才成为问题,即星期六时钟变了。我是否需要使用Azure提高票证?或者我做错了吗?

2 个答案:

答案 0 :(得分:1)

Azure App服务器正在使用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

答案 1 :(得分:0)

时区依赖于服务器位置

我遇到了同样的问题。我在新加坡有我的服务器,它曾经和我的区域印度发生冲突。

它也会影响时间和日期格式。

所以Better not use ToLocalTime() at the server方面。只有get the UTC time from the serverconvert to local time at the client方面。问题已解决!

希望这会有所帮助。谢谢。