Extending JavaScript's Date.parse to allow for DD/MM/YYYY (non-US formatted dates)?
- by Campbeln
I've come up with this solution to extending JavaScript's Date.parse function to allow for dates formatted in DD/MM/YYYY (rather then the American standard [and default] MM/DD/YYYY):
(function() {
var fDateParse = Date.parse;
Date.parse = function(sDateString) {
var a_sLanguage = ['en','en-us'],
a_sMatches = null,
sCurrentLanguage,
dReturn = null,
i
;
//#### Traverse the a_sLanguages (as reported by the browser)
for (i = 0; i < a_sLanguage.length; i++) {
//#### Collect the .toLowerCase'd sCurrentLanguage for this loop
sCurrentLanguage = (a_sLanguage[i] + '').toLowerCase();
//#### If this is the first English definition
if (sCurrentLanguage.indexOf('en') == 0) {
//#### If this is a definition for a non-American based English (meaning dates are "DD MM YYYY")
if (sCurrentLanguage.indexOf('en-us') == -1 && // en-us = English (United States) + Palau, Micronesia, Philippians
sCurrentLanguage.indexOf('en-ca') == -1 && // en-ca = English (Canada)
sCurrentLanguage.indexOf('en-bz') == -1 // en-bz = English (Belize)
) {
//#### Setup a oRegEx to locate "## ## ####" (allowing for any sort of delimiter except a '\n') then collect the a_sMatches from the passed sDateString
var oRegEx = new RegExp("(([0-9]{2}|[0-9]{1})[^0-9]*?([0-9]{2}|[0-9]{1})[^0-9]*?([0-9]{4}))", "i");
a_sMatches = oRegEx.exec(sDateString);
}
//#### Fall from the loop (as we've found the first English definition)
break;
}
}
//#### If we were able to find a_sMatches for a non-American English "DD MM YYYY" formatted date
if (a_sMatches != null) {
var oRegEx = new RegExp(a_sMatches[0], "i");
//#### .parse the sDateString via the normal Date.parse function, but replacing the "DD?MM?YYYY" with "YYYY/MM/DD" beforehand
//#### NOTE: a_sMatches[0]=[Default]; a_sMatches[1]=DD?MM?YYYY; a_sMatches[2]=DD; a_sMatches[3]=MM; a_sMatches[4]=YYYY
dReturn = fDateParse(sDateString.replace(oRegEx, a_sMatches[4] + "/" + a_sMatches[3] + "/" + a_sMatches[2]));
}
//#### Else .parse the sDateString via the normal Date.parse function
else {
dReturn = fDateParse(sDateString);
}
//####
return dReturn;
}
})();
In my actual (dotNet) code, I'm collecting the a_sLanguage array via:
a_sLanguage = '<% Response.Write(Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"]); %>'.split(',');
Now, I'm not certain my approach to locating "us-en"/etc. is the most proper. Pretty much it's just the US and current/former US influenced areas (Palau, Micronesia, Philippines) + Belize & Canada that use the funky MM/DD/YYYY format (I am American, so I can call it funky =). So one could rightly argue that if the Locale is not "en-us"/etc. first, then DD/MM/YYYY should be used. Thoughts?
As a side note... I "grew up" in PERL but it's been a wee while since I've done much heavy lifting in RegEx. Does that expression look right to everyone?
This seems like a lot of work, but based on my research this is indeed about the best way to go about enabling DD/MM/YYYY dates within JavaScript. Is there an easier/more betterer way?
PS- Upon re-reading this post just before submission... I've realized that this is more of a "can you code review this" rather then a question (or, an answer is embedded within the question). When I started writing this it was not my intention to end up here =)