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