简体   繁体   中英

DateTime.TryParse() fails in Windows 7

DateTime.TryParse fails in Windows 7, when we change the regional settings to Italian.I even tried TryParseExact but with no luck. Does anybody have any idea on this or came across this type of scenario?

Code is some thing 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 it returns false.

or

Even tried:

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

cb.Text is a String which contains the DateTime in string representation.

Try setting Thread Culture to Italian Culture using CreateSpecificCulture method.

See list of cultures here .

Have you tried calling it with a 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)

In Italian the time separator token is resolved to . rather than :

Try escaping the time separator token in single quotes for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM