Question

I have a nice function setup for formatting dates as per Leading Zero Date Format C#

But it turns out in our farm we have some blades running on a UK locale and some on a US locale so depending on which it crashes.

So what I'm after is how do I test the current server locale?

Something like...

if(Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName == "GB"){
...

}
else
{

..otherstatement
}

Ta

Was it helpful?

Solution

You should pass in the desired culture to all of your formatting functions (InvariantCulture, usually).

Alternatively, you can set the culture on the page like so. That code could also go in the Application BeginRequest override in your asax.cs file in order to affect all pages.

OTHER TIPS

Date now = DateTime.Now;
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
Console.WriteLine(now.ToString("d", ci));

Usually we go with UTC instead of locale specific code.

Testing for CurrentCulture is the only way to achieve what you ask for, as far as I know. However, I would strongly suggest to keep dates as DateTime until they are to be presented in the user interface, and not have if-statements in server functionality that is relying on date formats.

I usually try to apply date formatting as close to the user as possible.

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