Search code examples
c#.netdatetimeparsingglobalization

Parsing exact dates in C# shouldn't force you to create an IFormatProvider


Someone please correct me if I'm wrong, but parsing a yyyy/MM/dd (or other specific formats) dates in C# should be as easy as

DateTime.ParseExact(theDate, "yyyy/MM/dd");

but no, C# forces you to create an IFormatProvider.

Is there an app.config friendly way of setting this so I don't need to do this each time?

DateTime.ParseExact(theDate, "yyyy/MM/dd", new CultureInfo("en-CA", true));

Solution

  • The IFormatProvider argument can be null.