DateTime.Parse的奇怪行为

Ste*_*and 5 .net c# datetime

我们有一个大型应用程序(不幸的是)使用DateTime.Parse(文本)很多.用更好的东西替换DateTime.Parse现在不是一个选项.

最近我升级了Windows(到Windows 10)并开始出现问题.根据https://msdn.microsoft.com/en-us/library/system.datetime.parse(v=vs.110).aspx中的文档,该方法应使用CultureInfo.CurrentCulture作为默认的formatprovider.在我的电脑上似乎并非如此,除非我遗漏了一些明显的东西.

以下代码调用函数两次,首先不更改任何文化设置,然后在文化和ui文化上设置挪威文化.出于某种原因,它能够解析在两种情况下以en-US格式(M/d/yyyy)给出的文本,但是它无法以CultureInfo期望的格式解析日期.

这里发生了什么?我希望我错过了一些明显的东西......

class Program
{
    static void Main(string[] args)
    {
        var norwegianCulture = new CultureInfo("nb-NO");

        Console.WriteLine("First");
        ParseDates();

        Console.WriteLine("\nSecond");
        Thread.CurrentThread.CurrentUICulture = norwegianCulture;
        ParseDates();

        Console.ReadKey();
    }

    private static void ParseDates()
    {
        Console.WriteLine($"CultureInfo.CurrentCulture: {CultureInfo.CurrentCulture}");
        Console.WriteLine($"CultureInfo.CurrentUICulture: {CultureInfo.CurrentUICulture}");
        Console.WriteLine($"{Thread.CurrentThread.CurrentCulture} - CurrentCulture - {Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern}");
        Console.WriteLine($"{Thread.CurrentThread.CurrentUICulture} - CurrentUICulture - {Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern}");
        var shortDateString = new DateTime(2015, 9, 5).ToShortDateString();
        WriteParsedDate(shortDateString);
        WriteParsedDate("1/2/2015");
        Console.WriteLine();
    }

    private static void WriteParsedDate(string shortDateString)
    {
        try
        {
            var d = DateTime.Parse(shortDateString, CultureInfo.CurrentCulture);
            Console.WriteLine($"The text {shortDateString} parsed to Year: {d.Year}, Month: {d.Month}, Day: {d.Day}");
        }
        catch (Exception e)
        {
            Console.WriteLine($"The text {shortDateString} could not be parsed.");
            Console.WriteLine($"Error: {e.Message}");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将此内容写入控制台:

First
CultureInfo.CurrentCulture: nb-NO
CultureInfo.CurrentUICulture: en-US
nb-NO - CurrentCulture - dd.MM.yyyy
en-US - CurrentUICulture - M/d/yyyy
The text 05.09.2015 could not be parsed.
Error: String was not recognized as a valid DateTime.
The text 1/2/2015 parsed to Year: 2015, Month: 2, Day: 1


Second
CultureInfo.CurrentCulture: nb-NO
CultureInfo.CurrentUICulture: nb-NO
nb-NO - CurrentCulture - dd.MM.yyyy
nb-NO - CurrentUICulture - dd.MM.yyyy
The text 05.09.2015 could not be parsed.
Error: Strengen ble ikke gjenkjent som en gyldig DateTime.
The text 1/2/2015 parsed to Year: 2015, Month: 2, Day: 1
Run Code Online (Sandbox Code Playgroud)

部分解决方法

正如shf301所写,这是由于Windows 10中的一个错误.

我设法通过从"."更改时间分隔符来使其工作.到":" - 与旧版本的Windows一样.我猜它正在使用这个更改,因为我们只使用用户设置,而不是手动加载/指定它们.

shf*_*301 4

这是Connect列出的一个未解决的错误。对于任何具有相同日期和时间分隔符的区域设置,日期解析似乎都会失败

\n\n
Estonian (Estonia)\nFinnish (Finland)\nNorwegian Bokm\xc3\xa5l (Norway)\nNorwegian Bokm\xc3\xa5l (Svalbard and Jan Mayen)\nSerbian (Cyrillic, Kosovo)\nSerbian (Latin, Montenegro)\nSerbian (Latin, Serbia)\nSerbian (Latin, Kosovo)\n
Run Code Online (Sandbox Code Playgroud)\n\n

您的示例从不使用美国设置解析或格式化日期。首先DateTime.Parse没有IFormatProvider使用CurrentCulture和 not CurrentUICulture,其次你明确地传递CurrentCultureDateTime.Parse. 这就是为什么两种情况下的输出完全相同。

\n\n

如果"1/2/2015"使用规则进行解析en-US,则会输出:

\n\n
The text 1/2/2015 parsed to Year: 2015, Month: 1, Day: 2\n
Run Code Online (Sandbox Code Playgroud)\n\n

DateTime.Parse只是很聪明,假设您使用 a/作为日期分隔符而不是 a.并将日期解析为d/M/yyyy

\n\n

不幸的是,在这个问题得到解决之前,您将不得不将您的呼叫更改为DateTime.Parse。或者切换到不受影响的.Net 3.5 或更早版本。

\n

  • 根据以下帖子,该修复将在 9 月的 Windows 10 更新中发布:http://www.heikniemi.net/hardcoded/2015/08/windows-10-breaks-net-date-parsing-in-certain-locales / (2认同)