Convert time in string to UTC time?

Total Post:183

 2369  View(s)
Rate this:

I have a string like this "2012.12.04T08:35:00" that represents a time in the "W. Europe Standard Time" timezone.

Now, I want to convert this properly to a c# DateTime object in UTC time.

What is the proper way to do this?

  1. Post:412

    Re: Convert time in string to UTC time?

    Hi Ankita,

    Use TimeZoneInfo when converting between specific time zones:

    TimeZoneInfo westInfo =    TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
    DateTime westTime = DateTime.Parse("2012.12.04T08:35:00");
    DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(westTime, westInfo);

    To address your confusion:

    DateTime.Parse as used here makes no assumptions about the timezone of the given value. IT stores it with a DateTimeKind of Unspecified.

    TimeZoneInfo.ConvertTimeToUtc as used here expects an Unspecified datetime, reads it as if it is in the explicitly specified time zone, and converts it to UTC.

      Modified On Apr-04-2018 03:50:22 AM



Enter your email address here always to be updated. We promise not to spam!