views:

258

answers:

4

I have a nice function setup for formatting dates as per http://stackoverflow.com/questions/461098/leading-zero-date-format-c-asp-net

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

+1  A: 

Usually we go with UTC instead of locale specific code.

Kieveli
That would be my personal preference but basically I'm converting the date from UK to US to meet a 3rd party api requirement
Chris M
+1  A: 

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.

Fredrik Mörk
+2  A: 

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.

Michael Haren
Set the page culture; should of remembered this considering I had to do it to an entire classic ASP app a month ago.Thanks
Chris M
+2  A: 
Date now = DateTime.Now;
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
Console.WriteLine(now.ToString("d", ci));
HVS