How to Get Date and Time Formats Based on Culture Info

How can I get date and time formats based on Culture Info?

You can retrieve the format strings from the CultureInfo DateTimeFormat property, which is a DateTimeFormatInfo instance. This in turn has properties like ShortDatePattern and ShortTimePattern, containing the format strings:

CultureInfo us = new CultureInfo("en-US");
string shortUsDateFormatString = us.DateTimeFormat.ShortDatePattern;
string shortUsTimeFormatString = us.DateTimeFormat.ShortTimePattern;

CultureInfo uk = new CultureInfo("en-GB");
string shortUkDateFormatString = uk.DateTimeFormat.ShortDatePattern;
string shortUkTimeFormatString = uk.DateTimeFormat.ShortTimePattern;

If you simply want to format the date/time using the CultureInfo, pass it in as your IFormatter when converting the DateTime to a string, using the ToString method:

string us = myDate.ToString(new CultureInfo("en-US"));
string uk = myDate.ToString(new CultureInfo("en-GB"));

C#, datetime formatting, mont name, cultureinfo

I do not think the format you are looking for is the default format for all cultures. The code below will display the date in the default Full format for the specified culture. F is for FullDateTimePattern.

var now = DateTime.Now;
var denmark = now.ToString("F", new CultureInfo("da-DK"));

According to that line of code the output is:

12. marts 2017 03:34:12

So my takeaway from that is: The default full datetime pattern in Denmark is as the above. If you want another format, then use the code below and tweak it as you need to but keep in mind people in Denmark may find this pattern odd since they may not be used to it:

var denmarkCustomized = now.ToString("dddd, MMMM dd, yyyy h:mm:ss tt"
, new CultureInfo("da-DK"));

DateTime format and neutral culture

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.

Get CultureInfo from computer settings at run time and format a DateTime

Use CultureInfo.CurrentCulture static property.

When a thread is started, its culture is initially determined as
follows:

  • By retrieving the culture that is specified by the DefaultThreadCurrentCulture property in the application domain in
    which the thread is executing, if the property value is not null.
  • By calling the Windows GetUserDefaultLocaleName function.

And about DefaultThreadCurrentCulture:

Unless it is set explicitly, the value of the
DefaultThreadCurrentCulture property is null, and the culture of
threads in an application domain that have not been assigned an
explicit culture is defined by the default Windows system culture.

How to find the default dateformat of a Culture

I believe the issue is the way you're retrieving the desired culture.

This should give you the default culture settings:

var cultureLanguageTag = CultureInfo.CurrentCulture.IetfLanguageTag;
var defaultCulture = CultureInfo.GetCultureInfoByIetfLanguageTag( cultureLanguageTag );

Constructing one with a string is supposed to be used with the culture name, I don't know if it should work with the IETF tag, it may have just been returning the current culture in that case.

Changing Date format to en-us while culture is fr-ca

Surprisingly I got a very simple answer. While setting Culture and UICulture, all I need to do is to set the Culture Property to en-us always. This will result in showing the date format in English always.



Related Topics



Leave a reply



Submit