Set the DateFormat in Global.asax

How do you centrally control the DateFormat in your ASP.NET application? So for example if you want to display the date in a dd-MM-yyyy format on each page of your application without much efforts, how would you do it?

Use the Global.asax!

The following code:

Response.Write("Today's Date Is :" +
DateTime.Now.ToShortDateString());

produces the result shown below (mm/dd/yyyy):

image

Now to convert this dateformat 'centrally' to ‘dd-mm-yyyy’, follow these steps:

Create a new Global.asax file, if you do not have one in your web application. Import the following namespaces as shown here:

<%@ Import Namespace="System.Globalization" %>
<%
@ Import Namespace="System.Threading" %>

Now in the Application_BeginRequest(), write the following code:

C#

protected void Application_BeginRequest(object sender, EventArgs e)
{
CultureInfo cInfo = new CultureInfo("en-IN");
cInfo.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy";
cInfo.DateTimeFormat.DateSeparator = "-";
Thread.CurrentThread.CurrentCulture = cInfo;
Thread.CurrentThread.CurrentUICulture = cInfo;
}

VB.NET

Protected Sub Application_BeginRequest(ByVal sender As Object, _
ByVal e As EventArgs)
Dim cInfo As New CultureInfo("en-IN")
cInfo.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy"
cInfo.DateTimeFormat.DateSeparator = "-"
Thread.CurrentThread.CurrentCulture = cInfo
Thread.CurrentThread.CurrentUICulture = cInfo
End Sub

Now when you run the same piece of code in any of your pages

Response.Write("Today's Date Is :" +
DateTime.Now.ToShortDateString());

you get the following output:

image






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

2 comments:

Unknown said...

Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

Dim cInfo As New CultureInfo("en-IN")
cInfo.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"
cInfo.DateTimeFormat.DateSeparator = "/"
Thread.CurrentThread.CurrentCulture = cInfo
Thread.CurrentThread.CurrentUICulture = cInfo
MsgBox(cInfo)
End Sub
Hi,
This code where i need to place in vb.net?
I have placed this in code window. but its not effecting.
actually in my application I am following "dd/MM/yyyy" format. But others system having different kind of formats. I need to set my format in my application globally. It should not influence with any kind system date format.
Thank you..

Unknown said...

Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

Dim cInfo As New CultureInfo("en-IN")
cInfo.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"
cInfo.DateTimeFormat.DateSeparator = "/"
Thread.CurrentThread.CurrentCulture = cInfo
Thread.CurrentThread.CurrentUICulture = cInfo
MsgBox(cInfo)
End Sub

Hi,
You given code where i need to place in vb.net?
I have place in code window. But its not affecting.
My requirement is, in my application I am following "dd/MM/yyyy" format. But systems having different kind of date formats like "M/dd/yy". So I need to set my format globally for my application without influencing different kind of systems date formats.