1

I am trying to create a DateTime from a custom string however I am getting the error:

String was not recognized as a valid DateTime.

DateTime date = DateTime.ParseExact("Thu Feb 23 23:36:01 EST 2017", "ddd MMM dd hh:mm:ss K yyyy", CultureInfo.InvariantCulture);

Any suggestions on where I am going wrong?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
John Doe
  • 3,053
  • 17
  • 48
  • 75

1 Answers1

1

hh is for a 12 hour clock. You should use HH for a 24 hour clock. You'll also need to see the mentioned duplicate for better timezone handling.

When in doubt about parsing custom formats, check the documentation! See Custom Date and Time Format Strings on MSDN.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
  • So it appears that I can't use K for the EST. This is how they are sending me the date string. I will have to manipulate it some prior to converting it. – John Doe Feb 27 '17 at 16:20
  • Thanks on the HH catch. I didn't even noticed I had the wrong format there. – John Doe Feb 27 '17 at 16:21
  • @JDS I know it may not be under control, but if you can avoid the need to parse timezone abbreviations like `EST` that would be ideal. – mason Feb 27 '17 at 16:22