0

Here is my string "Wednesday 15th of April 2020 07:36:46 AM CDT".

Here is my code for parsing it:

var dtf = "dddd d MMMM yyyy HH:mm:ss tt zz";

var x = "Wednesday 15th of April 2020 07:36:46 AM CDT"
  .Replace("st ", " ")
  .Replace("th ", " ")
  .Replace(" of ", " ")
  .Trim();

DateTime.ParseExact(x, dtf, CultureInfo.InvariantCulture);

But it does not parse:

Unhandled exception. System.FormatException: String 'Wednesday 15 April 2020 07:36:46 AM CDT' was not recognized as a valid DateTime.

I think I have the format wrong.

How can I parse this date-time?

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
  • You'll have to do this without "CDT". Timezone abbreviations like that are ambiguous. – Hans Passant Apr 15 '20 at 14:31
  • You have two things to deal with outside of the date format: The "th" suffix and the time zone. Neither of those can be handled just with a format. You're going to have to do some preprocessing. You could also remove the "of" before hand or hard-code it in the format - either would work. – D Stanley Apr 15 '20 at 14:35
  • Here are some examples of date formats that should work: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parse?view=netframework-4.8 – Culme Apr 15 '20 at 14:36
  • FYI - since I read it in one of the comments beware of `August` - ends with `st` and you don't want to replace the `st` there. – Rand Random Apr 15 '20 at 14:45

0 Answers0