Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime conversion from Utc to Local in .NET 4.0

Tags:

c#

I have a function which among other things does a conversion from Utc to Local and vice-versa. 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.

ex: My current time zone is +2 UTC

MyCurrentTime is set to 27.09.2012, 19:00 and the DateTimeKind is Unspecified.

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

The output on Vista is 27.09.2012, 04:00

Any ideas why this happens?

Thanks.

like image 342
adi burus Avatar asked Nov 04 '22 15:11

adi burus


1 Answers

The solution was the one suggested by https://stackoverflow.com/users/570150/v4vendetta.

Both the Win 7 and Vista machines "agreed" on the correct time.

localTime= TimeZone.CurrentTimeZone.ToLocalTime(utcTime);

Thanks a lot!

like image 116
adi burus Avatar answered Nov 13 '22 16:11

adi burus