C# DateTime.TryParseExact not working as expected

114 views Asked by At

I hate to ask dumb questions but I've been through the MSDN article a couple times and trying stuff for an hour but no matter what I try I can't get this format to work. Here's what I'm trying to parse:

Thu, Jun 22

With

bool parsed = DateTime.TryParseExact("Thu, Jun 22", @"ddd, MMM dd", CultureInfo.Invariant, DateTimeStyles.None, out dateAndTime);

Using the format:

ddd, MMM dd

Removing the ddd, works so I'm pretty certain there's something with the first part, but I'm not sure what. I've tried using ' ' around the comma and escaping it with backslash, with no luck.

1

There are 1 answers

0
John Wu On BEST ANSWER

Since "Thu Jun 22" doesn't contain a year, the system will automatically infer the current year, so it will be treated as Thu Jun 22 2016.

Unfortunately, June 22, 2016 is not a Thursday, but actually a Friday. So you should get a FormatException with that value.

Try adding a year or removing the weekday.