DateTime.TryParse() returns different results on different computers with explicit culture

I am having a weird scenario where DateTime.TryParse(..) is returning different results with a custom culture (en-US) between two different laptops for a MM/dd/yyyy format.

Here is the code:

var isDateTime = DateTime.TryParse("07/22/2022", new CultureInfo("en-US"), DateTimeStyles.None, out _);

On my computer, the above returns false and on two other computers, the same statement is returning true. I am expecting this to return true, but I have no idea why it is returning false on my computer. Can anyone provide a clue on why setting the culture explicitly is not working for me?

EDIT – this is on .NET Framework V4.7.2 on Windows 10 with VS 2022.

>Solution :

There is CultureInfo.UseUserOverride property that might affect it on Windows. You can try disable user-overridden settings by using new CultureInfo("en-US", false) instead.

Also, the better alternative is using CultureInfo.GetCulture("en-US") instead because it always return non user-override version of CultureInfo, and it also returns a cached version which is usually faster than instantiating new CultureInfo every time.


Leave a Reply