日期时间格式和中性文化

DateTime format and neutral culture

根据文档Formatting Date and Time for a Specific Culture

The DateTime structure provides methods that allow your applications to perform culture-sensitive operations on a DateTime type. An application can use the DateTimeFormatInfo class to format and display a DateTime type based on culture. For example, using DateTimeFormatInfo.ShortDatePattern, the date February 1, 2001 can be formatted as 2/1/2001 for the English (United States), "en-US", culture and 01/02/2001 for the English (United Kingdom), "en-GB", culture.

An DateTimeFormatInfo object can be created for a specific culture or for the invariant culture, but not for a neutral culture. A neutral culture does not provide enough information to display the correct date format. An exception is thrown if the application attempts to create a DateTimeFormatInfo object using a neutral culture.

但是下面怎么不抛出异常呢?由于未指定特定文化,它从哪里获得有关如何格式化日期的信息?

Console.WriteLine(DateTime.Now.ToString("d", new CultureInfo("fr")));

更新:

您可以创建特定文化或中立文化的实例, 例如 new CultureInfo("fr-FR")new CultureInfo("fr").

DateTime.ToString方法中它说

The ToString(String) method returns the string representation of a date and time value in a specific format that uses the formatting conventions of the current culture; for more information, see CultureInfo.CurrentCulture.

并且 CultureInfo 文档说

The CultureInfo object that is returned by this property and its associated objects determine the default format for dates, times, numbers, currency values, the sorting order of text, casing conventions, and string comparisons.

更新 2

是的,所以上面引用的 MSDN 页面已过时并且框架行为已更改。

我有以下代码

var culture = new CultureInfo("en");
Console.WriteLine(DateTime.Now.ToString("d", culture));

当我 运行 此代码针对 .NET 3.0 时,出现以下异常。但它在 .NET 4.6.2 上运行良好。

An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll

Additional information: Culture 'en' is a neutral culture. It cannot be used in formatting and parsing and therefore cannot be set as the thread's current culture.

所以,回到我原来的问题。当我使用中性文化时,关于 DateTime 格式的信息来自哪里?

来自the most recent version of the MSDN doc on DateTimeFormatInfo

However, a neutral culture lacks culture-specific formatting information, because it is independent of a specific country/region. Instead of populating the DateTimeFormatInfo object with generic values, the .NET Framework returns a DateTimeFormatInfo object that reflects the formatting conventions of a specific culture that is a child of the neutral culture. For example, the DateTimeFormatInfo object for the neutral en culture reflects the formatting conventions of the en-US culture, and the DateTimeFormatInfo object for the fr culture reflects the formatting conventions of the fr-FR culture.

.NET 4 的 What's New in Globalization and Localization 文章中也对此进行了描述:

Previous versions of the .NET Framework threw an exception if applications tried to access neutral culture properties such as DateTimeFormatInfo.FirstDayOfWeek. In the .NET Framework 4, neutral culture properties return values that reflect the specific culture that is most dominant for that neutral culture. For example, the French neutral locale retrieves the values of most of its properties from French (France). The FirstDayOfWeek property returns DayOfWeek.Monday, which reflects the value of that property in the French (France) culture.