Thursday, 4 February 2010

Converting from local to UTC in C#

Its one of thse things that should be easy.
However if you get your DateTime from a database it will be anonymous, i.e. it will not have its kind set.
So, do this:

DateTime ConvertFromLocalToUTC(DateTime localTime)
DateTime anon = new DateTime(localTime.Ticks, DateTimeKind.Local);
DateTime dtUTC = anon.ToUniversalTime();
return dtUTC;

Oh, and, in other news, happy Imbolc!

No comments:

Post a Comment