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

6

66

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.

Strove answered 19/11, 2008 at 2:26 Comment(0)
P
118

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;
}
Patiencepatient answered 19/11, 2008 at 2:35 Comment(2)
For reference: Custom Date and Time Format StringsNannie
@Filip - 'dd-MMM-yyyy' is valid, and will give dates like '03-Jun-2016'.Checkbook
D
55

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

<system.web>
    <globalization  culture="en-NZ"  uiCulture="en-NZ"/>
</system.web>
Delmardelmer answered 8/5, 2009 at 0:47 Comment(0)
P
10

A good way is configure the Web.Config, the date format appear in all components of a aspx

<system.web>    
<globalization uiCulture="en" culture="en-NZ" />
</system.web>
Pageboy answered 6/11, 2013 at 2:18 Comment(0)
A
4

You can set your culture without manipulation:


using System.Globalization;
using System.Threading;

//... protected void Application_BeginRequest(Object sender, EventArgs e) {
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-NZ"); }

Aparicio answered 19/11, 2008 at 3:3 Comment(1)
I don't believe I want to change the culture since all environments are set to the same culture, "English (New Zealand)". The issue was with differing Date Separators. Our locals seemed to have been altered by a group policy or something to set the Date Separator to "-" instead of "/"Strove
B
4

It is an old post, but I thought it might be useful to mention it here, my problem was something like what the OP has asked, but the solution was that the culture was changed in the IIS, not from control panel.

IIS has it is own culture selection, which by default will follow the local computer culture. But for some reason, it was changed to a different culture, and I started getting those weird date formats.

You can look for more answers here.

Bethink answered 1/2, 2017 at 10:11 Comment(1)
Thank you so much. In my case the Netwok Service account was using another culture. This answer solved it for me: https://mcmap.net/q/112448/-how-to-set-date-and-time-format-in-iis-7Raymonraymond
L
0

For format strings, the format character / does not actually resolve to the literal "/" as you would expect. Instead, it resolves to the current date time separator as configured in your regional settings. Try changing the DateTimeFormatInfo.DateSeparator property.

For more information, see: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Lagniappe answered 19/11, 2008 at 3:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.