I have the following line of code in existing implementation
DateTime.TryParseExact(
"15/11/2021 00:00:00",
"dd/MM/yyyy HH:mm:ss",
null,
DateTimeStyles.None,
out maturityDate);
which returns false that means that the passed string couldn’t be parsed. This was really surprising for me because the pattern here seems to be accurate. According to MSDN null value within the third parameter means that the current culture info will be used (I assume it’s Thread.CurrentThread.CurrentCulture).
Thread.CurrentThread.CurrentCulture in the watch window is en-US, but the instance of culture info was altered somewhere in the code later (date time formatters or something else).
When I pass CultureInfo.InvariantCulture or new CultureInfo("en-US") everything is ok.
Could anyone please say what causes the failure of TryParseExact here when null is passed? The similar questions gave no clue to me.
If you pass
null, theCurrentCulturewill be used.From the MSDN documentation for
TryParseExact:This means that if the current culture is something that uses different date/time separators than in your string, the parse will fail.