Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.-ctor?view=net-6.0#system-globalization-cultureinfo-ctor(system-string-system-boolean)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading