Format String to Datetime with Timezone

Viewed 64414

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?

3 Answers

I convert my date string with timezone "Thu, 22 Sep 2022 06:38:58 +0200" using "ddd, dd MMM yyyy HH:mm:ss zzz":

var dateString = "Thu, 22 Sep 2022 06:38:58 +0200";
string[] acceptedDateFormats = { "ddd, dd MMM yyyy HH:mm:ss zzz" };

var dateParsed = DateTime.TryParseExact(dateString, acceptedDateFormats,
  CultureInfo.InvariantCulture, DateTimeStyles.None, out var date);
Related