简体   繁体   English

DateTime.TryParse 在不同机器上的工作方式不同

[英]DateTime.TryParse works differently in different machines

I'm using following code in my project but "DateTime.TryParse" gives different results in different machine.我在我的项目中使用以下代码,但“DateTime.TryParse”在不同的机器上给出了不同的结果。 IsDate method returns "False" on my machine and return "True" on another machine. IsDate 方法在我的机器上返回“False”,在另一台机器上返回“True”。

if(IsDate("30/03/2020 04:00",out dt))
{
}

private bool IsDate(object o, out DateTime date)
{
    return DateTime.TryParse(o.ToString(), CultureInfo.CurrentCulture, DateTimeStyles.None, out date);
}

I have also tried "DateTime.TryParseExact" as per in below article, but no use.我也按照下面的文章尝试了“DateTime.TryParseExact”,但没有用。 https://github.com/dotnet/runtime/issues/25120 https://github.com/dotnet/runtime/issues/25120

Please suggest me any ideas to make it work properly.请向我提出任何使其正常工作的想法。

Thanks.谢谢。

Replace CultureInfo.CurrentCulture with a fixed culture like for example CultureInfo.InvariantCulture :CultureInfo.CurrentCulture替换为固定的文化,例如CultureInfo.InvariantCulture

private bool IsDate(object o, out DateTime date)
{
    return DateTime.TryParse(o.ToString(), System.Globalization.CultureInfo.InvariantCulture, 
        DateTimeStyles.None, out date);
}

CultureInfo.CurrentCulture is a per-thread setting/property that defaults to the user settings on the machine. CultureInfo.CurrentCulture是每个线程的设置/属性,默认为机器上的用户设置。

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

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