sakura-bloom
sakura-bloom

Reputation: 4594

DateTime format and neutral culture

According to documentation 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.

But how come the following does not throw an exception? And where does it get information on how to format date, since specific culture was not specified?

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

Update:

You can create instances of specific culture or neutral culture, for example new CultureInfo("fr-FR") or new CultureInfo("fr").

In DateTime.ToString method it says

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.

And CultureInfo documentation says

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.

Update 2

Yes, so the MSDN page referenced above is outdated and the framework behaviour has changed.

I have the following code

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

When I run this code targeting .NET 3.0 I get the following exception. But it works well on .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.

So, going back to my original question. When I use neutral culture, where does the information about DateTime format come from?

Upvotes: 2

Views: 2868

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241450

From 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.

This is also described in the What's New in Globalization and Localization article for .NET 4:

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.

Upvotes: 4

Related Questions