DateTime.TryParse () error in Windows 7

DateTime.TryParsedoes not work on Windows 7 when we change the regional settings to Italian. I even tried TryParseExact, but no luck. Does anyone know about this or have come across this type of script?

The code is something like this:

string[] formats = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", "M/d/yyyy h:mm", "M/d/yyyy h:mm", "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm", "dd/MM/yyyy HH:mm"}; 
if (DateTime.TryParseExact(cb.Text, formats, CultureInfo.InVariantCulture, DateTimeStyles.AllowLeadingWhite, out date_and_time))

but returns false.

or

I even tried:

if (DateTime.TryParse(cb.Text, CultureInfo.InvariantCulture, DateTimeStyles.None,out date_and_time) == true)` 

cb.Text is a string that contains a string representation of a DateTime.

+3
source share
3 answers

Have you tried calling it neutral CultureInfo?

Like this

DateTime parsed;

if(DateTime.TryParse("2010-03-09", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsed))
    Console.WriteLine(parsed)

Or for TryParseExact

DateTime.TryParseExact("2010-03-09", "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsed)
0
source

In Italian, a time separator token is allowed. but not:

Try using a single-quote time separator marker, for example:

"M/d/yyyy h':'mm':'ss tt"
0

Italian Culture CreateSpecificCulture.

. .

0
source

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


All Articles