Question

Well, I am trying to get the default date format of a culture.

For example en-us has "m/dd/yyyy" , en-uk 'd/mm/yyyy'. Culture could be anything at the client machine. and Dateformat can also be anything. like for Culture is en-us but dateformat is dd-MMM-yyyy.

so this case I want to get the ShortDateformatString as m/dd/yyyy not dd-MMM-yyyy.

I tried do this for all the culture by using:

string _cultureInfo = CultureInfo.CurrentCulture.IetfLanguageTag;
CultureInfo shortDatefomatString = new CultureInfo(_cultureInfo);
string old = shortDatefomatString.DateTimeFormat.ShortDatePattern;

but it always returns mt the dd-MMM-yyyy. i need the default one not the set on the machine.

Was it helpful?

Solution

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.

OTHER TIPS

According to MSDN, CurrentCulture will return the culture in one of the following ways:

  1. It returns CultureInfo.DefaultThreadCurrentCulture if it is not null. (Note that unless you specifically set a culture, this will always be null.)
  2. It returns the result of calling Windows' GetUserDefaultLocaleName function. This will return whatever Culture you set in Control Panel.

It may be possible that if you change the culture in Control Panel, you will not see the changes until you restart your computer.

To test whether cultures are showing the correct dates, you can manually choose a culture instead of using the system culture by using CultureInfo.CreateSpecificCulture("en-US"); (replacing "en-US" with the culture codes of other countries).

The following seems to work for me.

Console.WriteLine(CultureInfo.GetCultureInfo("en-us").DateTimeFormat.ShortDatePattern);
Console.WriteLine(CultureInfo.GetCultureInfo("en-gb").DateTimeFormat.ShortDatePattern);

Output: M/d/yyyy dd/MM/yyyy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top