DateTime.TryParseExact () error due to thread culture information

I have the following line of code in an existing implementation

DateTime.TryParseExact( "15/11/2021 00:00:00", "dd/MM/yyyy HH:mm:ss", null, DateTimeStyles.None, out maturityDate); 

which returns false , which means that the past line cannot be parsed. It was really amazing for me, because here the picture seems accurate. According to MSDN, a null value in the third parameter means that the current culture information will be used (I assume it is Thread.CurrentThread.CurrentCulture ).

Thread.CurrentThread.CurrentCulture in the en-US viewport, but the culture information instance was changed somewhere in the code later (date time formats or something else).

When I go through CultureInfo.InvariantCulture or new CultureInfo("en-US") , everything is fine.

Can someone say what causes TryParseExact here when null passed? Such questions did not give me any clue.

+4
source share
2 answers

If you pass null , CurrentCulture will be used.

In the MSDN documentation for TryParseExact :

If the provider has nothing, the CultureInfo object corresponding to the current culture is used.

This means that if the current culture is something that uses different date and time separators than on your string, the analysis will not be performed.

+7
source

15/11/2021 is not a valid U.S. date format. 11/15/2021. I think the culture you want is en-GB

0
source

Source: https://habr.com/ru/post/1390438/


All Articles