Reputation: 4387
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
Upvotes: 0
Views: 1321
Reputation: 2275
Date now = DateTime.Now;
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
Console.WriteLine(now.ToString("d", ci));
Upvotes: 4
Reputation: 108246
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.
Upvotes: 2
Reputation: 158309
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.
Upvotes: 1