为什么当日期格式包含天数时,月份的缩写与不包含天数时的月份缩写不同?

问题描述 投票:0回答:2

运行 .Net 8 控制台应用程序时,为什么

MMM
当其属于日期格式(例如
.ToString("dd-MMM-yy")
)时显示“June”而不是“Jun”,而当它是日期格式时显示“Jun”(例如
.ToString("MMM")
)本身?

// Set culture to English (Australia)
var culture = new CultureInfo("en-AU");

// Get the current date
var currentDate = new DateTime(2024, 6, 8);

// Display the current date using the short date pattern
var formattedDate = currentDate.ToString("d", culture);
Console.WriteLine("Short date in en-AU culture: " + formattedDate); // Outputs: 8/06/2024

// Display the abbreviated month name separately
var abbreviatedMonth = currentDate.ToString("MMM", culture);
Console.WriteLine("Abbreviated month: " + abbreviatedMonth); // Outputs: Jun

var incorrect = currentDate.ToString("dd-MMM-yy", culture);
Console.WriteLine("Incorrect format: " + incorrect); // Outputs: 08-June-24

Windows 可以,但 C# 不行。请注意屏幕截图右下角的月份(我将 Windows 时间更改为六月)。

enter image description here

c# datetime formatting
2个回答
11
投票

DateTimeFormatInfo
中月份有两种缩写,一种叫
AbbreviatedMonthNames
,另一种叫
AbbreviatedMonthGenitiveNames
。六月在
AbbreviatedMonthNames
中的缩写是
Jun
,在
AbbreviatedMonthGenitiveNames
中是
June

文档中属格月份名称的解释如下:

在某些语言中,作为日期一部分的月份名称出现在所有格中。例如,ru-RU 或俄语(俄罗斯)文化中的日期由日数和属格月份名称组成,例如 1 Января(1 月 1 日)。

选择缩写的关键代码在这里:IsUseGenitiveForm,其注释为:

操作:检查格式,看看我们是否应该在格式中使用属格月份。从(format)字符串中的位置(index)开始,向后看并向前看是否有“d”或“dd”。在像“d MMMM”或“MMMM dd”这样的情况下,我们可以使用所有格形式。如果有两个以上的“d”,则不使用属格形式。

因此,如果格式中有

d
dd
,则会选择
June


1
投票

正如 shingo 已经指出的那样,该行为似乎起源于使用属性

DateTimeFormatInfo.AbbreviatedMonthNames
DateTimeFormatInfo.AbbreviatedMonthGenitiveNames
的逻辑。

但是,目前还没有提出解决问题的方案。

这两个属性具有公共设置器。操作这些数组以使它们具有预期的内容可能非常容易。

// Set culture to English (Australia)
var culture = new CultureInfo("en-AU");

// Workaround for unexpected month abbreviations:
culture.DateTimeFormat.AbbreviatedMonthGenitiveNames =
    new string[] { "Jan", "Feb", "Mar",
                   "Apr", "May", "Jun",
                   "Jul", "Aug", "Sep",
                   "Oct", "Nov", "Dec",
                   "" };

// Get the current date
var currentDate = new DateTime(2024, 6, 8);

// Display the current date using the short date pattern
var formattedDate = currentDate.ToString("d", culture);
Console.WriteLine("Short date in en-AU culture: " + formattedDate); // Outputs: 8/06/2024

// Display the abbreviated month name separately
var abbreviatedMonth = currentDate.ToString("MMM", culture);
Console.WriteLine("Abbreviated month: " + abbreviatedMonth); // Outputs: Jun

var fixedIncorrect = currentDate.ToString("dd-MMM-yy", culture);
Console.WriteLine("Fixed incorrect format: " + fixedIncorrect); // Outputs: 08-Jun-24  :-)

我不清楚为什么 .NET 的某些版本会为 en-AU 文化的这些月份缩写公开不同的值。在 .NET Framework 4.7.2 中,它默认工作正常。因此,新版本的 .NET 似乎确实存在问题,因为澳大利亚日期/时间格式设置政策本身不会在一夜之间发生变化。

© www.soinside.com 2019 - 2024. All rights reserved.