Home > DeveloperSection > Forums > Convert time in string to UTC time?
Ankita Pandey
Ankita Pandey

Total Post:183

Posted on    January-31-2014 11:36 PM

 C# C# 

 1 Reply(s)
 1573  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?

Pravesh Singh

Total Post:412

Posted on    January-31-2014 11:46 PM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick