How to find the default dateformat of a Culture
Asked Answered
H

3

11

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.

Helles answered 13/12, 2012 at 2:10 Comment(0)
D
4

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.

Decrypt answered 13/12, 2012 at 2:41 Comment(1)
This works and gives the right output. the default dateformat.Helles
K
1

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

Katydid answered 13/12, 2012 at 2:39 Comment(1)
CreateSpecificCulture also give the current setting only. Thanks for the answer.Helles
T
1

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

Teamwork answered 13/12, 2012 at 3:35 Comment(1)
in case of current culture is same and Shortdate format is different. output would be different.Helles

© 2022 - 2024 — McMap. All rights reserved.