使用DateTime.ToString时,Windows 10中的时间输出(AM / PM)发生了变化(" tt")

时间:2015-08-08 01:50:52

标签: c# datetime datetime-format windows-10

我最近升级到了Windows 10 - 我现在看到使用" tt"时的日期输出发生了一些意想不到的变化。格式说明符。

以下是一些演示此问题的代码:

using System.IO;
using System;
using System.Globalization;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        var cultures = new string[] {null, "en-NZ", "en-US", "en-AU", "en-GB"};

            foreach (var culture in cultures) {
                if (culture != null) {
                    var c = CultureInfo.GetCultureInfo(culture);
                    System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = c;
                }

                DateTime dt = new DateTime(2015, 1, 2, 3, 4, 5, DateTimeKind.Utc);

                Console.WriteLine("selection: {0} CurrentThread.CurrentCulture.Name: {1} CurrentThread.CurrentUICulture.Name: {2}  Value: {3}",
                    culture ?? "ambient",
                    System.Threading.Thread.CurrentThread.CurrentCulture.Name,
                    System.Threading.Thread.CurrentThread.CurrentUICulture.Name,
                    dt.ToString("hhh:mm tt"));
            }
    }
}

以前版本的Windows中的输出是:

selection: ambient CurrentThread.CurrentCulture.Name: en-NZ CurrentThread.CurrentUICulture.Name: en-NZ Value: 03:04 a.m.
selection: en-NZ CurrentThread.CurrentCulture.Name: en-NZ CurrentThread.CurrentUICulture.Name: en-NZ Value: 03:04 a.m.
selection: en-US CurrentThread.CurrentCulture.Name: en-US CurrentThread.CurrentUICulture.Name: en-US Value: 03:04 AM
selection: en-AU CurrentThread.CurrentCulture.Name: en-AU CurrentThread.CurrentUICulture.Name: en-AU Value: 03:04 AM
selection: en-GB CurrentThread.CurrentCulture.Name: en-GB CurrentThread.CurrentUICulture.Name: en-GB Value: 03:04 am

在Windows 10中:

selection: ambient (windows 10) CurrentThread.CurrentCulture.Name: en-NZ CurrentThread.CurrentUICulture.Name: en-US  Value: 03:04 a.m.
selection: en-NZ CurrentThread.CurrentCulture.Name: en-NZ CurrentThread.CurrentUICulture.Name: en-NZ  Value: 03:04 AM
selection: en-US CurrentThread.CurrentCulture.Name: en-US CurrentThread.CurrentUICulture.Name: en-US  Value: 03:04 AM
selection: en-AU CurrentThread.CurrentCulture.Name: en-AU CurrentThread.CurrentUICulture.Name: en-AU  Value: 03:04 AM
selection: en-GB CurrentThread.CurrentCulture.Name: en-GB CurrentThread.CurrentUICulture.Name: en-GB  Value: 03:04 AM

在这两种情况下,此代码都是编译获胜的Visual Studio 2013,目标是.Net Framework 4.5

有没有人知道为什么行为发生了变化 - 为什么在Windows 10中看起来在线程上设置任何文化都会特别改变AM / PM的输出格式为" AM" /" PM"不是通常输出的文化?

3 个答案:

答案 0 :(得分:6)

你可能想看看"文化数据不应该被认为是稳定的" http://blogs.msdn.com/b/shawnste/archive/2005/04/05/405694.aspx

有一点是我们开始利用CLDR(通用语言环境数据存储库,http://cldr.unicode.org)来处理许多语言环境,以更好地符合行业标准。您可能已经注意到,我们现在有很多其他语言环境可供您使用。

答案 1 :(得分:0)

在将DateTime更改为使用ToString("G")函数的字符串时,我总是强制使用ToString()。然后我没有问题再次将字符串解析为DateTime。我在Windows 7/8/10上使用我的客户端应用程序(以“G”格式获取日期和时间,并将其解析为DateTime)进行测试。

答案 2 :(得分:0)

I also found that the combination of 'en-nz' and Windows 10 upgrade suddenly change a.m./p.m.to AM/PM.

The difference is the code I maintained relaying on ToShortTimeString() output with a.m./p.m. but in Windows 10 it changes to AM/PM instead I spend ages trying to find the correct culture info/format string, and I am also trying to find the missing local setting on my new Windows 10 machine compare to my old Windows 7 machine. But it turns out that the same local setting on different version of Windows machine does indeed display a.m./p.m. AM/PM differently.

Setting on my Windows 10 machine And the AM Symbol changed to AM now Setting on a Windows 7 machine in comparison And the AM Symbol is a.m. for the same Windows display language And same setting on Windows 2012 in comparison which confirm that so far only windows 10 are different

同时确认{en-nz'的DateTimeFormatInfo.AMDesignator现在输出'AM'而不是'a.m。'