[英]DateTime conversion from Utc to Local in .NET 4.0
I have a function which among other things does a conversion from Utc to Local and vice-versa. 我有一个功能,其中包括从Utc到Local的转换,反之亦然。 The problem is that when I run it on a PC with Win 7 it works OK, but when I run it on a PC with Vista the conversion goes wrong.
问题是,当我在装有Win 7的PC上运行它时,它运行正常,但是当我在装有Vista的PC上运行它时,转换会出错。
ex: My current time zone is +2 UTC 例如:我当前的时区是+2 UTC
MyCurrentTime is set to 27.09.2012, 19:00 and the DateTimeKind is Unspecified. MyCurrentTime设置为27.09.2012,19:00,DateTimeKind未指定。
DateTime utcTime = DateTime.SpecifyKind(MyCurrentTime,DateTimeKind.Utc);
DateTime localTime = new DateTime();
localTime = utcTime.Date.ToLocalTime();
The output on Win 7 is 27.09.2012, 17:00 Win 7的输出是27.09.2012,17:00
The output on Vista is 27.09.2012, 04:00 Vista上的输出是27.09.2012,04:00
Any ideas why this happens? 任何想法为什么会这样?
Thanks. 谢谢。
The solution was the one suggested by https://stackoverflow.com/users/570150/v4vendetta . 该解决方案是https://stackoverflow.com/users/570150/v4vendetta建议的解决方案。
Both the Win 7 and Vista machines "agreed" on the correct time. Win 7和Vista机器都在正确的时间“达成一致”。
localTime= TimeZone.CurrentTimeZone.ToLocalTime(utcTime);
Thanks a lot! 非常感谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.