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



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

2 comments:

prasad babu 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..

prasad babu 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.