3

i have to parse this string below into a datetime object in C#:

Wed, 13 Apr 2011 07:11:04 -0400 (EDT)

what is the simplest way of doing this?

I understand there is DateTime.Parse and DateTime.ParseExact but i am trying to figure out what the custom format syntax would be for this above.

leora
  • 188,729
  • 360
  • 878
  • 1,366

1 Answers1

3

You need to use DateTime.ParseExact and pass in a custom format.
Something like:

var parsed = DateTime.ParseExact("Wed, 13 Apr 2011 07:11:04 -0400 (EDT)", 
                                 "ddd, dd MMM yyyy HH:mm:ss zzz", null);

Note
Time zone abbreviations are not supported as there is no official designation of them and they are sometimes ambiguous.
You should strip this from the input to parse the above. You could look at parsing that yourself if you know what the possible values will be.

leora
  • 188,729
  • 360
  • 878
  • 1,366
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • basically my question is what would the custom format syntax be to parse the above. – leora Apr 14 '11 at 00:02
  • i don't see this overload. all overload seem to require a FormatProvider as well – leora Apr 14 '11 at 01:55
  • @ooo you can pass `null` or `CultureInfo.InvariantCulture` if you don't really want to pass a format provider. - Sorry entered code from memory. – Matt Lacey Apr 14 '11 at 08:24