1

Need to convert this string: Mon Oct 31 16:18:15 CDT 2011

Into a valid DateTime value.
Have tried every variation of the date time styles with DateTime.Parseto no avail.

Any ideas?

JohnFx
  • 34,542
  • 18
  • 104
  • 162
user569963
  • 31
  • 3

1 Answers1

3

The problem is with the CDT you have there. This is not a valid portion of a string representing a DateTime.

You may have luck with replacing this with a valid representation of a timezone -0500 and the K format specifier for it.


You can use the following format string to parse the string:

ddd MMM dd HH:mm:ss CDT yyyy

For instance:

DateTime.ParseExact("Mon Oct 31 16:18:15 CDT 2011", 
                    "ddd MMM dd HH:mm:ss CDT yyyy", 
                    CultureInfo.InvariantCulture);

I suggest reading the documentation for Custom Date and Time Format Strings on MSDN.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Yeah you will have to replace that CDT with the offset as Oded has said. – Jeremy Jan 23 '12 at 22:04
  • Thanks - I will read. That ParseExact handled the CDT just fine. – user569963 Jan 23 '12 at 22:51
  • @user569963 - Note that `ParseExact` will throw an exception if not successful. There is a `TryParseExact` that will not throw an exception but return a boolean value indicating success or failure. – Oded Jan 23 '12 at 22:52