Convert DateTime from Utc to Local in .NET 4.0

I have a function that, among other things, does the conversion from Utc to Local and vice versa. The problem is that when I run it on a PC with Win 7, it works fine, but when I run it on a PC with Vista, the conversion goes wrong.

ex: My current time zone is +2 UTC

MyCurrentTime is set on 09/27/2012, 7:00 p.m., but DateTimeKind is not specified.

DateTime utcTime = DateTime.SpecifyKind(MyCurrentTime,DateTimeKind.Utc);  
DateTime localTime = new DateTime();                             
localTime = utcTime.Date.ToLocalTime();

Exit to Win 7 - 09/27/2012, 5:00 p.m.

Exit to Vista - 09/27/2012, 04:00 a.m.

Any ideas why this is happening?

Thank.

+5
source share
1 answer

The solution was what was proposed by https://stackoverflow.com/users/570150/v4vendetta .

Both Win 7 and Vista computers β€œagreed” at the right time.

localTime= TimeZone.CurrentTimeZone.ToLocalTime(utcTime);

Thank you so much!

+2
source

All Articles