Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format String to Datetime with Timezone

I have a string s = "May 16, 2010 7:20:12 AM CDT that i want to convert into a DateTime object. In the code below i get a Date format cannot be converted error when attempting to parse the text with a known format.

timeStamp = matches[0].Groups[1].Value;
dt = DateTime.ParseExact(timeStamp, "MMM dd, yyyy H:mm:ss tt", null);

The timezone comes in as CDT UTC... and i think is whats causing the problem or my format?

like image 817
Warz Avatar asked Aug 08 '12 21:08

Warz


1 Answers

Central Daylight Time

Try this:

string dts = "May 16, 2010 7:20:12 AM CDT";
DateTime dt = 
    DateTime.ParseExact(dts.Replace("CDT", "-05:00"), "MMM dd, yyyy H:mm:ss tt zzz", null);

EDIT:

For daylight savings time please consider DateTime.IsDaylightSavingTime and TimeZone.CurrentTimeZone

Custom Date and Time Format Strings

like image 168
rumburak Avatar answered Sep 22 '22 06:09

rumburak