mam wymyślić tego rozwiązania powiększenia Date.parse
funkcji javascript, aby umożliwić terminach sformatowanych w DD/MM/RRRR (raczej wtedy American Standard [i domyślne] MM/DD/RRRR):Przedłużanie JavaScript Date.parse, aby umożliwić DD/MM/YYYY (daty w formacie innym niż USA)?
(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
sCurrentLanguage.indexOf('en-ca') == -1 && // en-ca = English (Canada)
sCurrentLanguage.indexOf('en-ph') == -1 && // en-ph = English (Philippians)
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;
}
})();
w moim rzeczywistego (dotNet) kod, jestem zbieranie tablicę a_sLanguage poprzez:
a_sLanguage = '<% Response.Write(Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"]); %>'.split(',');
Teraz, nie jestem pewien, moje podejście do lokalizowania "Us-en"/etc. jest najbardziej odpowiedni. W zasadzie to tylko USA i obecne/dawne strefy wpływów USA (Palau, Mikronezja, Filipiny) + Belize & Kanada, które używają funky formatu MM/DD/RRRR (jestem Amerykaninem, więc mogę go nazwać funky =). Można więc słusznie argumentować, że jeśli Locale nie jest "en-us"/etc. najpierw należy użyć DD/MM/YYYY. Myśli?
Na marginesie ... "dorastałem" w PERL-ie, ale to było bardzo małe, od kiedy zrobiłem wiele ciężkich ćwiczeń w RegEx. Czy to wyrażenie pasuje do wszystkich?
Wydaje się, że to dużo pracy, ale na podstawie moich badań jest to najlepszy sposób na włączenie dat DD/MM/YYYY w JavaScript. Czy istnieje łatwiejszy/lepszy sposób?
PS- Po ponownym przeczytaniu tego wpisu tuż przed przesłaniem ... Zdałem sobie sprawę, że jest to raczej "możesz to sprawdzić recenzji" zamiast pytania (lub, odpowiedź jest osadzona w pytaniu). Kiedy zacząłem pisać to nie było moim zamiarem skończyć tutaj =)
Używany ten kod do wtyczki kalendarza jQuery. Dzięki za udostępnienie! Możesz to sprawdzić tutaj: https://github.com/joelalejandro/jquery-ja/wiki/ja.Calendar –