CONVERT TIME IN STRING TO UTC TIME?

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
C# 
C#
 1984  View(s)
Ratings:
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. Pravesh Singh

    Post:412

    Points:2888
    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.

Answer

NEWSLETTER

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