为什么 GetShortestDayName 返回的名称比预期的要短?

Why is GetShortestDayName returning a shorter name than expected?

DateTimeFormatInfo.GetShortestDayName(DayOfWeek) 应该是 return 名称,如 SuMo 等,如 Microsoft documentation 上示例的输出所示。

相反,我只收到一个字母:SM

重现代码:(我使用的是 WPF .Net 6)

DateTimeFormatInfo en_us = CultureInfo.GetCultureInfo("en-US").DateTimeFormat;
string s = en_us.GetShortestDayName(DayOfWeek.Tuesday);

这对我没有用,因为例如星期二和星期四具有相同的标识符。什么可能导致此问题,以便我可以修复它并获得预期的 SuMo 等?

这似乎是从 .NET 4.7.2 到 .NET Core 6 的更改(可能是所有版本的 .NET Core - 我现在无法确认)。

在 .NET 4.7.2 中我得到 two-character 个结果;在 .NET 6 中,我得到 one-character 个结果。

我也没有看到任何在线文档表明该更改是有意的重大更改。

我会提交错误票以更新文档(并承认这是一个重大更改)或恢复行为。

解决方法是使用 GetAbbreviatedDayName 的前两个字符。

这可能是“设计使然”的结果,是从 Windows-specific 获取全球化信息的方式转换为公开可用数据的方式。

下面的文章 Breaking change: Globalization APIs use ICU libraries on Windows 解释了这个变化(但确实没有像“如果短名称被改变”这样的细节):

Starting in .NET 5, if an app is running on Windows 10 May 2019 Update or later, .NET libraries use ICU globalization APIs, by default.

并且 ICU site 将缩写列为组成部分之一(突出显示是我的):

Formatting: Format numbers, dates, times and currency amounts according the conventions of a chosen locale. This includes translating month and day names into the selected language, choosing appropriate abbreviations, ordering fields correctly, etc. This data also comes from the Common Locale Data Repository.

您可以将以下设置添加到您的 (.csproj) 项目文件中,以不使用 .NET 5 中引入的 ICU globalization APIs

<ItemGroup>
    <RuntimeHostConfigurationOption Include="System.Globalization.UseNls" Value="true" />
</ItemGroup>