views:

1246

answers:

3

In an ASP.NET application I'm setting the culture to lv-LV. All is fine, but the default short date format seems to be "yyyy.mm.dd". The client wants it to be "dd.mm.yyyy" (which is actually the LV standard). Where does ASP.NET get the date/time settings for a specific culture? How can they be changed for a single culture, without affecting other cultures (the application is supposed to be localizable)?

+3  A: 

I once had a similar problem and solved it with:

DateTimeFormatInfo dtfi = (DateTimeFormatInfo)Thread.CurrentThread.CurrentCulture.DateTimeFormat.Clone();
dtfi.ShortDatePattern = "dd.MM.yyyy";
dtfi.DateSeparator = ".";
Thread.CurrentThread.CurrentCulture.DateTimeFormat = dtfi ;
Thread.CurrentThread.CurrentUICulture.DateTimeFormat = dtfi;

That way you get to keep every other regional setting except the date format.

This code actually ran on OnLoad in our Page base class. I'm not so sure that is the best way.

Jonas Elfström
Something like that crossed my mind too. Still it seems somehow... wrong. :D I don't like hardcoding. Perhaps there are some sytem registry keys that need to be changed for that culture or something?
Vilx-
The problem with that is if some of your users prefer the ISO standard date format.
Jonas Elfström
A: 

Check out this link: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

There is a class called Culture and UICulture.

Jack Marchetti
<sarcasm>No shit! I never thought of that! Awesome! Now, if only I could figure out where these classes get their stuff from...</sarcasm> Besides - the class is called CultureInfo. These are two properties of a thread (and a whole lot of other things).
Vilx-
A: 

.Net gets the date/time settings for a specific culture from the DateTimeFormatInfo class. You can set the DateTimeFormat property of a CultureInfo with a DateTimeFormatInfo that you have modified. If that culture is the current culture, then you can set it on the CurrentCulture. Either way, this only affects the CultureInfo that you have created and won't affect any other culture. (Any call to CultureInfo.GetCultureInfo("lv-LV") will not pick up those changes either since that is a read-only copy.) You don't necessarily have to pass that CultureInfo to any DateTime formatting methods (ToString, ParseExact) - you can pass the explicit pattern or the DateTimeFormatInfo.

CultureInfo lvLV = new CultureInfo("lv-LV");

DateTimeFormatInfo lvLVdtfi = (DateTimeFormatInfo) lvLV.DateTimeFormat.Clone();
lvLVdtfi.ShortDatePattern = "dd.MM.yyyy";
lvLV.DateTimeFormat = lvLVdtfi;

DateTime.Now.ToString("d", lvLV);     // short date pattern from modified lv-LV CultureInfo
DateTime.Now.ToString("d", lvLVdtfi); // short date pattern from modified DateTimeFormatInfo
DateTime.Now.ToString("dd.MM.yyyy");  // explicit short date pattern

If lv-LV is the current Windows culture, then you can set a user override (use intl.cpl the Regional and Language Options control panel and customize the format) and the framework will pick this up.

Eric MSFT