« A camera of the future | Main | Updating from SilverLight 2 Beta 1 to Beta 2 »
Wednesday
Jun252008

Missing entry: setting DateTimeFormat.ShortDatePattern across the entire ASP.NET application

This entry is about a week overdue, nonetheless I shall post it now:

In the global.asax, before page execute, do this:

protected override void PrePageExecute(Page page)
{
// change short date pattern of the current culture short date pattern
CultureInfo info = (CultureInfo)CultureInfo.CurrentCulture.Clone();
info.DateTimeFormat.ShortDatePattern = "dd MMM yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = info;
base.PrePageExecute(page);
}



I'm using CompositeWeb.WebClientApplication here, if you are using vanilla ASP.NET, you can use a whole lot of different events such as BeginRequest.


Take the current culture, clone it (because the current culture is read-only).  Then set the short date pattern to something universal like "dd MMM yyyy";


This affects all instances where these are used:


datetime.ToString("d");
datetime.ToShortDateString();
datetime.ToShortDateTimeString();  // which is a concatenation of "d" and "t"
datetime.ToString();


This fixes issues for our application that is used by both American (MM/dd/yyyy) and Australian (dd/MM/yyyy) audiences.


Ideally, the user would set the language setting in their browser to the appropriate locale (en-US or en-AU), but this is too hard to force upon our non-technical users and the risk of someone misreading a date is too high, so we've opted for the "dd MMM yyyy" format which everybody understands.


jliu

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>