简体   繁体   English

Windows 7中的DateTime.TryParse()失败

[英]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. 当我们将区域设置更改为意大利语时, DateTime.TryParse在Windows 7中失败。我甚至尝试了TryParseExact但没有运气。 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. 但它返回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. cb.Text是一个String,它包含字符串表示形式的DateTime。

Try setting Thread Culture to Italian Culture using CreateSpecificCulture method. 尝试使用CreateSpecificCulture方法将Thread Culture设置为Italian Culture

See list of cultures here . 在此处查看文化列表。

Have you tried calling it with a neutral CultureInfo? 您是否尝试过使用中性CultureInfo调用它?

Like this 像这样

DateTime parsed;

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

Or for TryParseExact 或者对于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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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