Mark Richman Mark Richman - 1 year ago 96
C# Question

Convert UTC DateTime to another Time Zone

I have a UTC DateTime value coming from a database record. I also have a user-specified time zone (an instance of TimeZoneInfo). How do I convert that UTC DateTime to the user's local time zone? Also, how do I determine if the user-specified time zone is currently observing DST? I'm using .NET 3.5.


dtb dtb
Answer Source

Have a look at the DateTimeOffset structure:

// user-specified time zone
TimeZoneInfo southPole =
    TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");

// an UTC DateTime
DateTime utcTime = new DateTime(2007, 07, 12, 06, 32, 00, DateTimeKind.Utc);

// DateTime with offset
DateTimeOffset dateAndOffset =
    new DateTimeOffset(utcTime, southPole.GetUtcOffset(utcTime));


For DST see the TimeZoneInfo.IsDaylightSavingTime method.

bool isDst = southpole.IsDaylightSavingTime(DateTime.UtcNow);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download