I would like to set the date format to 'YYYY-MM-DD' for my entire web application. I can do this by creating an object of CultureAndRegionInfoBuilder
and then registering it. But since my application is on a hosted environment, I cannot run executables to register the culture.
I need a way to do this within the Application_Start
so that it applies to the entire web app.
I tried changing the date format using Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern
, but it does not seem to propagate the change to the entire application.
In short, I need a way to change current culture settings of the entire web application programatically from with in the web app itself.
Edit: I tried the following on suggestion of @doitgood, but got exception System.InvalidOperationException: Instance is read-only.
, while the intellisense shows the property as Get or Set.
void Application_BeginRequest(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
}
To make the change global to the application I tried the following in Application_Start
:
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
I get the same exception 'Instance is read-only'.
The answer by Sebeto is correct. I had the same problem and I just tried it. Thought I'd add some more explanation. The current culture's properties are indeed read-only once the current culture has been set. You must therefore construct a new CultureInfo object (easiest way is by cloning the existing culture). Then, you can tweak the culture info object's properties before swapping the current threads culture object with your own.
CultureInfo newCultureDefinition = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
//make percentages n% instead of n % .ToString("p") adds a space by default english culture in asp.net
newCultureDefinition.NumberFormat.PercentPositivePattern = 1;
newCultureDefinition.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
Thread.CurrentThread.CurrentCulture = newCultureDefinition;
There is also the question of where to put the code to change the current thread's culture.
According to Microsoft's example, see http://support.microsoft.com/kb/306162 you can even change the current culture on Page_Load. Since you might have code that needs your formatting before Page_Load, I have put my change in my base page OnInit.
protected override void OnInit(EventArgs e)
{
initGlobalCulturalFormattingChanges();
}
and to cap it off:
private void initGlobalCulturalFormattingChanges()
{
CultureInfo newCultureDefinition = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
//make percentages n% instead of n % (.ToString("p0") adds a space by default in asp.net
newCultureDefinition.NumberFormat.PercentPositivePattern = 1;
newCultureDefinition.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
Thread.CurrentThread.CurrentCulture = newCultureDefinition;
}
Is it the best idea to swap out the current Thread's culture with every request one every page's init? I don't have enough data to weigh in on that but it seems to work ok :).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With