Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you globally set the date format in ASP.NET?

How do you globally set the date format in ASP.NET?

My local machine and servers have Regional Settings set to "English (New Zealand)".

When I format a date with dd/MM/yyyy I expect to see 19/11/2008 for today for example.

Until recently, that is what I did in fact get from both my local machine and the servers.

Just recently, for some mysterious reason, our local machines have changed ever so slightly. Despite still be set to "English (New Zealand)", the date delimter has changed from / to -! The same change has not occurred on the servers which still show "English (New Zealand)" and the / for the date delimter.

So now for my local machine, for the format dd/MM/yyyy I get 19-11-2008 instead of 19/11/2008.

This is a little disconcerting.

The only way around it that I can see so far is to escape the slashes and set the format to dd\/MM\/yyyy. It seems to work, but it doesn't seem to be the ideal solution.

Can anyone please help?

NOTE: This is for an intranet application and I do not care about true globalisation. I just want to fix the date format and not have it change on me.

like image 272
BlackMael Avatar asked Nov 19 '08 02:11

BlackMael


People also ask

How can get date in dd mm yyyy format in asp net?

Solution 1Text=System. DateTime. Now. ToString("dd/MM/yyyy");

How do I change the date format in Visual Studio?

In "Control Panel\All Control Panel Items\Region and Language", on the "Formats" tab, click "Additional settings..." button. Visual Studio 2008 debugger formats dates using "Short date:" from the "Date" tab of "Customize Format".


2 Answers

You can change the current thread culture in your Global.asax file, and override the date format for example:

using System.Globalization; using System.Threading;  //... protected void Application_BeginRequest(Object sender, EventArgs e) {       CultureInfo newCulture = (CultureInfo) System.Threading.Thread.CurrentThread.CurrentCulture.Clone();   newCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";   newCulture.DateTimeFormat.DateSeparator = "-";   Thread.CurrentThread.CurrentCulture = newCulture; } 
like image 154
Christian C. Salvadó Avatar answered Oct 07 '22 06:10

Christian C. Salvadó


In web.config, set tag as per the following documentation

<system.web>     <globalization  culture="en-NZ"  uiCulture="en-NZ"/> </system.web> 
like image 37
Serapth Avatar answered Oct 07 '22 06:10

Serapth