diff --git a/README.md b/README.md index b006028..692df70 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ The constructor accepts two optional arguments: "MMMM d, yyyy, 'at' h 'o''clock'". If no pattern is provided, the default is "M/d/yy" for the locales en_US and es_US, or "d/M/yy" for - en_CA, fr_CA, en_GB, and en_AU. + en_CA, fr_CA, en_GB, en_AU and de_DE. * locale @@ -59,7 +59,7 @@ The constructor accepts two optional arguments: "d 'de' MMMM 'de' yyyy" will return a date such as "6 de julio de 2012". If locale is fr_CA, "'le' d MMMM yyyy k'h'mm" will return a date such as "le 6 juillet 2012 13h00". - Currently supported values are "en_US", "es_US", "en_CA", "fr_CA", "en_GB", and "en_AU". The default + Currently supported values are "en_US", "es_US", "en_CA", "fr_CA", "en_GB", "en_AU", "de_DE". The default is "en_US". The applyPattern Method diff --git a/SimpleDateFormat.js b/SimpleDateFormat.js index 3998ca5..4b393c9 100644 --- a/SimpleDateFormat.js +++ b/SimpleDateFormat.js @@ -1,2 +1,2 @@ -/* SimpleDateFormatJS | Version: 1.3 (25-JUL-2013) */ -function simpleDateFormat(e,t){this.locale=t&&(t=="es_US"||t=="en_CA"||t=="fr_CA"||t=="en_GB"||t=="en_AU")?t:"en_US";this.applyPattern=function(e){this.pattern=e||(this.locale=="en_CA"||this.locale=="fr_CA"||this.locale=="en_GB"||this.locale=="en_AU"?"d/M/yy":"M/d/yy")};this.applyPattern(e);this.format=function(e){e=e||new Date;if(!(e instanceof Date)){var n=e.split("T")[0].split("-"),r=e.split("T").length>1?e.split("T")[1].split(".")[0].split("Z")[0].split("-")[0].split(":"):["00","00","00"];e=new Date(n[0],n[1]-1,n[2],r[0],r[1],r[2])}var i=function(e){e=""+e;return e.indexOf("0")==0&&e!="0"?e.substring(1):e},s=function(e){e=Number(e);return isNaN(e)?"00":(e<10?"0":"")+e},o={month:s(e.getMonth()+1),date:s(e.getDate()),year:s(e.getFullYear()),day:e.getDay(),hour24:e.getHours(),hour12:e.getHours(),minutes:s(e.getMinutes()),ampm:"AM"};if(o.hour24>11){o.ampm="PM"}o.hour24=s(o.hour24);if(o.hour12==0){o.hour12=12}if(o.hour12>12){o.hour12=o.hour12-12}o.hour12=s(o.hour12);var u,a=function(e){var n=e.replace(/yy+(?=y)/g,"yy").replace(/MMM+(?=M)/g,"MMM").replace(/d+(?=d)/g,"d").replace(/EEE+(?=E)/g,"EEE").replace(/a+(?=a)/g,"").replace(/k+(?=k)/g,"k").replace(/h+(?=h)/g,"h").replace(/m+(?=m)/g,"m"),r=n.replace(/yyy/g,o.year).replace(/yy/g,o.year.substring(2)).replace(/y/g,o.year).replace(/dd/g,o.date).replace(/d/g,i(o.date)),u=function(e,t,n){for(var r=1;r23){i=23}var u=n=="+"?i:0-i;if(t=="kk"||t=="k"){u=Number(o.hour24)+u;if(u>24){u=u-24}else if(u<0){u=u+24}}else{u=Number(o.hour12)+u;if(u>24){u=u-24}else if(u<0){u=u+24}if(u>12){u=u-12}}u=""+u;if(t=="kk"||t=="hh"){u=s(u)}if(t=="h"&&u==0||t=="hh"&&u=="00"){u="12"}e[r]=u+e[r]}}return e.join("")};if(r.indexOf("k+")!=-1){r=u(r.split("kk+"),"kk","+");r=u(r.split("k+"),"k","+")}if(r.indexOf("k-")!=-1){r=u(r.split("kk-"),"kk","-");r=u(r.split("k-"),"k","-")}r=r.replace(/kk/g,o.hour24).replace(/k/g,i(o.hour24));if(r.indexOf("h+")!=-1){r=u(r.split("hh+"),"hh","+");r=u(r.split("h+"),"h","+")}if(r.indexOf("h-")!=-1){r=u(r.split("hh-"),"hh","-");r=u(r.split("h-"),"h","-")}r=r.replace(/hh/g,o.hour12<12&&o.hour12.indexOf&&o.hour12.indexOf("0")!=0?"0"+o.hour12:o.hour12).replace(/h/g,i(o.hour12));r=r.replace(/mm/g,o.minutes).replace(/m/g,i(o.minutes));r=r.replace(/a/g,"A");var a=["January","February","march","april","may","June","July","august","September","October","November","December"];if(t=="es_US"){a=["enero","febrero","marzo","abril","mayo","junio","julio","agosto","septiembre","octubre","noviembre","diciembre"]}if(t=="fr_CA"){a=["janvier","f&"+"#233;vrier","mars","avril","mai","juin","juillet","ao&"+"#251;t","septembre","octobre","novembre","d&"+"#233;cembre"]}r=r.replace(/MMMM/g,a[Number(o.month)-1]).replace(/MMM/g,a[Number(o.month)-1].substring(0,3)).replace(/MM/g,o.month).replace(/M/g,i(o.month)).replace(/march/g,"March").replace(/may/g,"May").replace(/Mayo/g,"mayo");var f=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];if(t=="es_US"){f=["domingo","lunes","martes","mi&"+"eacute;rcoles","jueves","viernes","s&"+"aacute;bado"]}if(t=="fr_CA"){f=["dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi"]}r=r.replace(/EEEE/g,f[o.day]).replace(/EEE/g,f[o.day].substring(0,3)).replace(/EE/g,f[o.day].substring(0,3)).replace(/E/g,f[o.day].substring(0,3));r=r.replace(/A/g,o.ampm).replace(/april/g,"April").replace(/august/g,"August");return r};if(this.pattern.indexOf("'")==-1){u=a(this.pattern)}else{var f=this.pattern.replace(/\'+(?=\')/g,"''").split("''");if(f.length==1){f=this.pattern.split("'");for(var l=0;l1?e.split("T")[1].split(".")[0].split("Z")[0].split("-")[0].split(":"):["00","00","00"];e=new Date(a[0],a[1]-1,a[2],t[0],t[1],t[2])}var i=function(e){return e=""+e,0==e.indexOf("0")&&"0"!=e?e.substring(1):e},n=function(e){return e=Number(e),isNaN(e)?"00":(10>e?"0":"")+e},l={month:n(e.getMonth()+1),date:n(e.getDate()),year:n(e.getFullYear()),day:e.getDay(),hour24:e.getHours(),hour12:e.getHours(),minutes:n(e.getMinutes()),ampm:"AM"};l.hour24>11&&(l.ampm="PM"),l.hour24=n(l.hour24),0==l.hour12&&(l.hour12=12),l.hour12>12&&(l.hour12=l.hour12-12),l.hour12=n(l.hour12);var s,u=function(e){var a=e.replace(/yy+(?=y)/g,"yy").replace(/MMM+(?=M)/g,"MMM").replace(/d+(?=d)/g,"d").replace(/EEE+(?=E)/g,"EEE").replace(/a+(?=a)/g,"").replace(/k+(?=k)/g,"k").replace(/h+(?=h)/g,"h").replace(/m+(?=m)/g,"m"),t=a.replace(/yyy/g,l.year).replace(/yy/g,l.year.substring(2)).replace(/y/g,l.year).replace(/dd/g,l.date).replace(/d/g,i(l.date)),s=function(e,r,a){for(var t=1;t23&&(i=23);var s="+"==a?i:0-i;"kk"==r||"k"==r?(s=Number(l.hour24)+s,s>24?s-=24:0>s&&(s+=24)):(s=Number(l.hour12)+s,s>24?s-=24:0>s&&(s+=24),s>12&&(s-=12)),s=""+s,("kk"==r||"hh"==r)&&(s=n(s)),("h"==r&&0==s||"hh"==r&&"00"==s)&&(s="12"),e[t]=s+e[t]}return e.join("")};-1!=t.indexOf("k+")&&(t=s(t.split("kk+"),"kk","+"),t=s(t.split("k+"),"k","+")),-1!=t.indexOf("k-")&&(t=s(t.split("kk-"),"kk","-"),t=s(t.split("k-"),"k","-")),t=t.replace(/kk/g,l.hour24).replace(/k/g,i(l.hour24)),-1!=t.indexOf("h+")&&(t=s(t.split("hh+"),"hh","+"),t=s(t.split("h+"),"h","+")),-1!=t.indexOf("h-")&&(t=s(t.split("hh-"),"hh","-"),t=s(t.split("h-"),"h","-")),t=t.replace(/hh/g,l.hour12<12&&l.hour12.indexOf&&0!=l.hour12.indexOf("0")?"0"+l.hour12:l.hour12).replace(/h/g,i(l.hour12)),t=t.replace(/mm/g,l.minutes).replace(/m/g,i(l.minutes)),t=t.replace(/a/g,"A");var u=["January","February","march","april","may","June","July","august","September","October","November","December"];"es_US"==r&&(u=["enero","febrero","marzo","abril","mayo","junio","julio","agosto","septiembre","octubre","noviembre","diciembre"]),"fr_CA"==r&&(u=["janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre"]),"de_DE"==r&&(u=["Januar","Februar","märz","april","mai","Juni","Juli","august","September","Oktober","November","Dezember"]),t=t.replace(/MMMM/g,u[Number(l.month)-1]).replace(/MMM/g,u[Number(l.month)-1].substring(0,3)).replace(/MM/g,l.month).replace(/M/g,i(l.month)).replace(/march/g,"March").replace(/may/g,"May").replace(/Mayo/g,"mayo").replace(/märz/g,"März"),"de_DE"==r&&(t=t.replace(/mai/g,"Mai"));var o=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];return"es_US"==r&&(o=["domingo","lunes","martes","miércoles","jueves","viernes","sábado"]),"fr_CA"==r&&(o=["dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi"]),"de_DE"==r&&(o=["Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag"]),t=t.replace(/EEEE/g,o[l.day]).replace(/EEE/g,o[l.day].substring(0,3)).replace(/EE/g,o[l.day].substring(0,3)).replace(/E/g,o[l.day].substring(0,3)),t=t.replace(/A/g,l.ampm).replace(/april/g,"April").replace(/august/g,"August")};if(-1!=this.pattern.indexOf("'")){var o=this.pattern.replace(/\'+(?=\')/g,"''").split("''");if(1==o.length){o=this.pattern.split("'");for(var h=0;h 1) ? unformattedDate.split('T')[1] - .split('.')[0] - .split('Z')[0] - .split('-')[0] - .split(':') - : ['00', '00', '00']; - unformattedDate = new Date(unformattedDateParts[0], (unformattedDateParts[1] - 1), - unformattedDateParts[2], unformattedDateTimeParts[0], - unformattedDateTimeParts[1], unformattedDateTimeParts[2]); - } - - /* - * oneDigitNumber is a private method which removes leading zeroes from the provided number. The - * method accepts one argument: - * - * num (required): - * The number to be formatted. - */ - var oneDigitNumber = function(num) { - num = '' + num; - return (num.indexOf('0') == 0 && num != '0') ? num.substring(1) : num; - }, - + console.log(locale); + this.locale = (locale && (locale == 'es_US' || locale == 'en_CA' || locale == 'fr_CA' || + locale == 'en_GB' || locale == 'en_AU' || locale == 'de_DE')) ? locale : 'en_US'; + /* - * twoDigitNumber is a private method which ensures the provided number has at least two digits. The - * method accepts one argument: + * applyPattern is a public method which updates the pattern. It accepts one argument: * - * num (required): - * The number to be formatted. - */ - twoDigitNumber = function(num) { - num = Number(num); - return isNaN(num) ? '00' : (((num < 10) ? '0' : '') + num); - }, - - /* - * The dateParts object contains each piece of the provided Date needed to construct the formatted - * date/time String. Each of the Numbers are converted to a String, with the exception of E. + * updatedPattern (optional): + * The updated pattern to apply. */ - dateParts = { - month: twoDigitNumber(unformattedDate.getMonth() + 1), - date: twoDigitNumber(unformattedDate.getDate()), - year: twoDigitNumber(unformattedDate.getFullYear()), - day: unformattedDate.getDay(), - hour24: unformattedDate.getHours(), - hour12: unformattedDate.getHours(), - minutes: twoDigitNumber(unformattedDate.getMinutes()), - ampm: 'AM' + this.applyPattern = function(updatedPattern) { + this.pattern = updatedPattern || ((this.locale == 'en_CA' || this.locale == 'fr_CA' || + this.locale == 'en_GB' || this.locale == 'en_AU' || this.locale == 'de_DE') ? + 'd/M/yy' : 'M/d/yy'); }; - if(dateParts.hour24 > 11) { - dateParts.ampm = 'PM'; - } - dateParts.hour24 = twoDigitNumber(dateParts.hour24); - if(dateParts.hour12 == 0) { - dateParts.hour12 = 12; - } - if(dateParts.hour12 > 12) { - dateParts.hour12 = dateParts.hour12 - 12; - } - dateParts.hour12 = twoDigitNumber(dateParts.hour12); - - var formattedDate, - + this.applyPattern(pattern); + /* - * patternReplace is a private method which interprets a piece of the provided pattern. + * format is a public method which returns the provided Date object as a String formatted based on the + * defined pattern. The method accepts one argument: * - * patternPart (required): - * The piece of the pattern to be interpretted. + * unformattedDate (optional): + * The date to be formatted. This can be either a Date object, or an ISO-8601 string. If no date is + * provided, a new Date object is created at the time the method is called. */ - patternReplace = function(patternPart) { - var patternPartFormatted = patternPart.replace(/yy+(?=y)/g, 'yy') - .replace(/MMM+(?=M)/g, 'MMM') - .replace(/d+(?=d)/g, 'd') - .replace(/EEE+(?=E)/g, 'EEE') - .replace(/a+(?=a)/g, '') - .replace(/k+(?=k)/g, 'k') - .replace(/h+(?=h)/g, 'h') - .replace(/m+(?=m)/g, 'm'), - - formattedPart = patternPartFormatted.replace(/yyy/g, dateParts.year) - .replace(/yy/g, dateParts.year.substring(2)) - .replace(/y/g, dateParts.year) - .replace(/dd/g, dateParts.date) - .replace(/d/g, oneDigitNumber(dateParts.date)), - - /* - * adjustTimePattern is a private method which adjusts the hour of the provided date up or down. - * - * timeParts (required): - * The pieces of the pattern split on a plus or minus sign after the hour pattern letters. - * - * timePatternPart (required): - * The type of pattern letter. One of "h", "hh", "k", or "kk". - * - * operator (required): - * The mathematical operator, either "+" or "-". - */ - adjustTimePattern = function(timeParts, timePatternPart, operator) { - for(var i = 1; i < timeParts.length; i++) { - if(!isNaN(timeParts[i].substring(0, 1))) { - var timePartOperand = timeParts[i].substring(0, 2); - timeParts[i] = timeParts[i].substring(2); - if(isNaN(timePartOperand.substring(1))) { - timeParts[i] = timePartOperand.substring(1) + timeParts[i]; - timePartOperand = timePartOperand.substring(0, 1); - } - timePartOperand = Number(timePartOperand); - if(timePartOperand > 23) { - timePartOperand = 23; - } - - var timePartResult = (operator == '+') ? timePartOperand : (0 - timePartOperand); - if(timePatternPart == 'kk' || timePatternPart == 'k') { - timePartResult = (Number(dateParts.hour24) + timePartResult); - if(timePartResult > 24) { - timePartResult = timePartResult - 24; - } - else if(timePartResult < 0) { - timePartResult = timePartResult + 24; - } + this.format = function(unformattedDate) { + unformattedDate = unformattedDate || new Date(); + if(!(unformattedDate instanceof Date)) { + var unformattedDateParts = unformattedDate.split('T')[0].split('-'), + unformattedDateTimeParts = (unformattedDate.split('T').length > 1) ? unformattedDate.split('T')[1] + .split('.')[0] + .split('Z')[0] + .split('-')[0] + .split(':') + : ['00', '00', '00']; + unformattedDate = new Date(unformattedDateParts[0], (unformattedDateParts[1] - 1), + unformattedDateParts[2], unformattedDateTimeParts[0], + unformattedDateTimeParts[1], unformattedDateTimeParts[2]); + } + + /* + * oneDigitNumber is a private method which removes leading zeroes from the provided number. The + * method accepts one argument: + * + * num (required): + * The number to be formatted. + */ + var oneDigitNumber = function(num) { + num = '' + num; + return (num.indexOf('0') == 0 && num != '0') ? num.substring(1) : num; + }, + + /* + * twoDigitNumber is a private method which ensures the provided number has at least two digits. The + * method accepts one argument: + * + * num (required): + * The number to be formatted. + */ + twoDigitNumber = function(num) { + num = Number(num); + return isNaN(num) ? '00' : (((num < 10) ? '0' : '') + num); + }, + + /* + * The dateParts object contains each piece of the provided Date needed to construct the formatted + * date/time String. Each of the Numbers are converted to a String, with the exception of E. + */ + dateParts = { + month: twoDigitNumber(unformattedDate.getMonth() + 1), + date: twoDigitNumber(unformattedDate.getDate()), + year: twoDigitNumber(unformattedDate.getFullYear()), + day: unformattedDate.getDay(), + hour24: unformattedDate.getHours(), + hour12: unformattedDate.getHours(), + minutes: twoDigitNumber(unformattedDate.getMinutes()), + ampm: 'AM' + }; + if(dateParts.hour24 > 11) { + dateParts.ampm = 'PM'; + } + dateParts.hour24 = twoDigitNumber(dateParts.hour24); + if(dateParts.hour12 == 0) { + dateParts.hour12 = 12; + } + if(dateParts.hour12 > 12) { + dateParts.hour12 = dateParts.hour12 - 12; + } + dateParts.hour12 = twoDigitNumber(dateParts.hour12); + + var formattedDate, + + /* + * patternReplace is a private method which interprets a piece of the provided pattern. + * + * patternPart (required): + * The piece of the pattern to be interpretted. + */ + patternReplace = function(patternPart) { + var patternPartFormatted = patternPart.replace(/yy+(?=y)/g, 'yy') + .replace(/MMM+(?=M)/g, 'MMM') + .replace(/d+(?=d)/g, 'd') + .replace(/EEE+(?=E)/g, 'EEE') + .replace(/a+(?=a)/g, '') + .replace(/k+(?=k)/g, 'k') + .replace(/h+(?=h)/g, 'h') + .replace(/m+(?=m)/g, 'm'), + + formattedPart = patternPartFormatted.replace(/yyy/g, dateParts.year) + .replace(/yy/g, dateParts.year.substring(2)) + .replace(/y/g, dateParts.year) + .replace(/dd/g, dateParts.date) + .replace(/d/g, oneDigitNumber(dateParts.date)), + + /* + * adjustTimePattern is a private method which adjusts the hour of the provided date up or down. + * + * timeParts (required): + * The pieces of the pattern split on a plus or minus sign after the hour pattern letters. + * + * timePatternPart (required): + * The type of pattern letter. One of "h", "hh", "k", or "kk". + * + * operator (required): + * The mathematical operator, either "+" or "-". + */ + adjustTimePattern = function(timeParts, timePatternPart, operator) { + for(var i = 1; i < timeParts.length; i++) { + if(!isNaN(timeParts[i].substring(0, 1))) { + var timePartOperand = timeParts[i].substring(0, 2); + timeParts[i] = timeParts[i].substring(2); + if(isNaN(timePartOperand.substring(1))) { + timeParts[i] = timePartOperand.substring(1) + timeParts[i]; + timePartOperand = timePartOperand.substring(0, 1); + } + timePartOperand = Number(timePartOperand); + if(timePartOperand > 23) { + timePartOperand = 23; + } + + var timePartResult = (operator == '+') ? timePartOperand : (0 - timePartOperand); + if(timePatternPart == 'kk' || timePatternPart == 'k') { + timePartResult = (Number(dateParts.hour24) + timePartResult); + if(timePartResult > 24) { + timePartResult = timePartResult - 24; + } + else if(timePartResult < 0) { + timePartResult = timePartResult + 24; + } + } + else { + timePartResult = (Number(dateParts.hour12) + timePartResult); + if(timePartResult > 24) { + timePartResult = timePartResult - 24; + } + else if(timePartResult < 0) { + timePartResult = timePartResult + 24; + } + if(timePartResult > 12) { + timePartResult = timePartResult - 12; + } + } + timePartResult = '' + timePartResult; + if(timePatternPart == 'kk' || timePatternPart == 'hh') { + timePartResult = twoDigitNumber(timePartResult); + } + if((timePatternPart == 'h' && timePartResult == 0) || (timePatternPart == 'hh' && + timePartResult == '00')) { + timePartResult = '12'; + } + timeParts[i] = timePartResult + timeParts[i]; + } + } + + return timeParts.join(''); + }; + + if(formattedPart.indexOf('k+') != -1) { + formattedPart = adjustTimePattern(formattedPart.split('kk+'), 'kk', '+'); + formattedPart = adjustTimePattern(formattedPart.split('k+'), 'k', '+'); + } + if(formattedPart.indexOf('k-') != -1) { + formattedPart = adjustTimePattern(formattedPart.split('kk-'), 'kk', '-'); + formattedPart = adjustTimePattern(formattedPart.split('k-'), 'k', '-'); + } + + formattedPart = formattedPart.replace(/kk/g, dateParts.hour24) + .replace(/k/g, oneDigitNumber(dateParts.hour24)); + + if(formattedPart.indexOf('h+') != -1) { + formattedPart = adjustTimePattern(formattedPart.split('hh+'), 'hh', '+'); + formattedPart = adjustTimePattern(formattedPart.split('h+'), 'h', '+'); + } + if(formattedPart.indexOf('h-') != -1) { + formattedPart = adjustTimePattern(formattedPart.split('hh-'), 'hh', '-'); + formattedPart = adjustTimePattern(formattedPart.split('h-'), 'h', '-'); + } + + formattedPart = formattedPart.replace(/hh/g, ((dateParts.hour12 < 12 && dateParts.hour12.indexOf && + dateParts.hour12.indexOf('0') != 0) ? ('0' + + dateParts.hour12) : + dateParts.hour12)) + .replace(/h/g, oneDigitNumber(dateParts.hour12)); + + formattedPart = formattedPart.replace(/mm/g, dateParts.minutes) + .replace(/m/g, oneDigitNumber(dateParts.minutes)); + + formattedPart = formattedPart.replace(/a/g, 'A'); + + var formattedMonthNames = ['January', + 'February', + 'march', + 'april', + 'may', + 'June', + 'July', + 'august', + 'September', + 'October', + 'November', + 'December']; + if(locale == 'es_US') { + formattedMonthNames = ['enero', + 'febrero', + 'marzo', + 'abril', + 'mayo', + 'junio', + 'julio', + 'agosto', + 'septiembre', + 'octubre', + 'noviembre', + 'diciembre']; + } + if(locale == 'fr_CA') { + formattedMonthNames = ['janvier', + 'février', + 'mars', + 'avril', + 'mai', + 'juin', + 'juillet', + 'août', + 'septembre', + 'octobre', + 'novembre', + 'décembre']; + } + if(locale == 'de_DE') { + formattedMonthNames = ['Januar', + 'Februar', + 'märz', + 'april', + 'mai', + 'Juni', + 'Juli', + 'august', + 'September', + 'Oktober', + 'November', + 'Dezember']; + } + formattedPart = formattedPart.replace(/MMMM/g, formattedMonthNames[Number(dateParts.month) - 1]) + .replace(/MMM/g, formattedMonthNames[Number(dateParts.month) - 1] + .substring(0, 3)) + .replace(/MM/g, dateParts.month) + .replace(/M/g, oneDigitNumber(dateParts.month)) + .replace(/march/g, 'March') + .replace(/may/g, 'May') + .replace(/Mayo/g, 'mayo') + .replace(/märz/g, 'März'); + + if(locale == 'de_DE'){ + formattedPart = formattedPart.replace(/mai/g, 'Mai'); + } + + var formattedDayNames = ['Sunday', + 'Monday', + 'Tuesday', + 'Wednesday', + 'Thursday', + 'Friday', + 'Saturday']; + if(locale == 'es_US') { + formattedDayNames = ['domingo', + 'lunes', + 'martes', + 'miércoles', + 'jueves', + 'viernes', + 'sábado']; + } + if(locale == 'fr_CA') { + formattedDayNames = ['dimanche', + 'lundi', + 'mardi', + 'mercredi', + 'jeudi', + 'vendredi', + 'samedi']; + } + if(locale == 'de_DE') { + formattedDayNames = ['Sonntag', + 'Montag', + 'Dienstag', + 'Mittwoch', + 'Donnerstag', + 'Freitag', + 'Samstag']; + } + + formattedPart = formattedPart.replace(/EEEE/g, formattedDayNames[dateParts.day]) + .replace(/EEE/g, formattedDayNames[dateParts.day].substring(0, 3)) + .replace(/EE/g, formattedDayNames[dateParts.day].substring(0, 3)) + .replace(/E/g, formattedDayNames[dateParts.day].substring(0, 3)); + + formattedPart = formattedPart.replace(/A/g, dateParts.ampm) + .replace(/april/g, 'April') + .replace(/august/g, 'August'); + + return formattedPart; + }; + + /* + * If the pattern does not contain any quoted strings, parse it as-is. + */ + if(this.pattern.indexOf('\'') == -1) { + formattedDate = patternReplace(this.pattern); + } + + /* + * If the pattern contains one or more quoted strings, split it into pieces and parse only the + * non-quoted pattern letters. + */ + else { + var formatPatternParts = this.pattern.replace(/\'+(?=\')/g, '\'\'').split('\'\''); + if(formatPatternParts.length == 1) { + formatPatternParts = this.pattern.split('\''); + for(var i = 0; i < formatPatternParts.length; i++) { + if(i % 2 == 0) { + formatPatternParts[i] = patternReplace(formatPatternParts[i]); + } + } + return formatPatternParts.join(''); } else { - timePartResult = (Number(dateParts.hour12) + timePartResult); - if(timePartResult > 24) { - timePartResult = timePartResult - 24; - } - else if(timePartResult < 0) { - timePartResult = timePartResult + 24; - } - if(timePartResult > 12) { - timePartResult = timePartResult - 12; - } - } - timePartResult = '' + timePartResult; - if(timePatternPart == 'kk' || timePatternPart == 'hh') { - timePartResult = twoDigitNumber(timePartResult); + for(var i = 0; i < formatPatternParts.length; i++) { + var formatPatternParts2 = formatPatternParts[i].split('\''); + for(var j = 0; j < formatPatternParts2.length; j++) { + if(j % 2 == 0) { + formatPatternParts2[j] = patternReplace(formatPatternParts2[j]); + } + } + formatPatternParts[i] = formatPatternParts2.join(''); + } + return formatPatternParts.join('\''); } - if((timePatternPart == 'h' && timePartResult == 0) || (timePatternPart == 'hh' && - timePartResult == '00')) { - timePartResult = '12'; - } - timeParts[i] = timePartResult + timeParts[i]; - } } - - return timeParts.join(''); - }; - - if(formattedPart.indexOf('k+') != -1) { - formattedPart = adjustTimePattern(formattedPart.split('kk+'), 'kk', '+'); - formattedPart = adjustTimePattern(formattedPart.split('k+'), 'k', '+'); - } - if(formattedPart.indexOf('k-') != -1) { - formattedPart = adjustTimePattern(formattedPart.split('kk-'), 'kk', '-'); - formattedPart = adjustTimePattern(formattedPart.split('k-'), 'k', '-'); - } - - formattedPart = formattedPart.replace(/kk/g, dateParts.hour24) - .replace(/k/g, oneDigitNumber(dateParts.hour24)); - - if(formattedPart.indexOf('h+') != -1) { - formattedPart = adjustTimePattern(formattedPart.split('hh+'), 'hh', '+'); - formattedPart = adjustTimePattern(formattedPart.split('h+'), 'h', '+'); - } - if(formattedPart.indexOf('h-') != -1) { - formattedPart = adjustTimePattern(formattedPart.split('hh-'), 'hh', '-'); - formattedPart = adjustTimePattern(formattedPart.split('h-'), 'h', '-'); - } - - formattedPart = formattedPart.replace(/hh/g, ((dateParts.hour12 < 12 && dateParts.hour12.indexOf && - dateParts.hour12.indexOf('0') != 0) ? ('0' + - dateParts.hour12) : - dateParts.hour12)) - .replace(/h/g, oneDigitNumber(dateParts.hour12)); - - formattedPart = formattedPart.replace(/mm/g, dateParts.minutes) - .replace(/m/g, oneDigitNumber(dateParts.minutes)); - - formattedPart = formattedPart.replace(/a/g, 'A'); - - var formattedMonthNames = ['January', - 'February', - 'march', - 'april', - 'may', - 'June', - 'July', - 'august', - 'September', - 'October', - 'November', - 'December']; - if(locale == 'es_US') { - formattedMonthNames = ['enero', - 'febrero', - 'marzo', - 'abril', - 'mayo', - 'junio', - 'julio', - 'agosto', - 'septiembre', - 'octubre', - 'noviembre', - 'diciembre']; - } - if(locale == 'fr_CA') { - formattedMonthNames = ['janvier', - 'f&' + '#233;vrier', - 'mars', - 'avril', - 'mai', - 'juin', - 'juillet', - 'ao&' + '#251;t', - 'septembre', - 'octobre', - 'novembre', - 'd&' + '#233;cembre']; - } - formattedPart = formattedPart.replace(/MMMM/g, formattedMonthNames[Number(dateParts.month) - 1]) - .replace(/MMM/g, formattedMonthNames[Number(dateParts.month) - 1] - .substring(0, 3)) - .replace(/MM/g, dateParts.month) - .replace(/M/g, oneDigitNumber(dateParts.month)) - .replace(/march/g, 'March') - .replace(/may/g, 'May') - .replace(/Mayo/g, 'mayo'); - - var formattedDayNames = ['Sunday', - 'Monday', - 'Tuesday', - 'Wednesday', - 'Thursday', - 'Friday', - 'Saturday']; - if(locale == 'es_US') { - formattedDayNames = ['domingo', - 'lunes', - 'martes', - 'mi&' + 'eacute;rcoles', - 'jueves', - 'viernes', - 's&' + 'aacute;bado']; - } - if(locale == 'fr_CA') { - formattedDayNames = ['dimanche', - 'lundi', - 'mardi', - 'mercredi', - 'jeudi', - 'vendredi', - 'samedi']; - } - formattedPart = formattedPart.replace(/EEEE/g, formattedDayNames[dateParts.day]) - .replace(/EEE/g, formattedDayNames[dateParts.day].substring(0, 3)) - .replace(/EE/g, formattedDayNames[dateParts.day].substring(0, 3)) - .replace(/E/g, formattedDayNames[dateParts.day].substring(0, 3)); - - formattedPart = formattedPart.replace(/A/g, dateParts.ampm) - .replace(/april/g, 'April') - .replace(/august/g, 'August'); - - return formattedPart; + + return formattedDate; }; - - /* - * If the pattern does not contain any quoted strings, parse it as-is. - */ - if(this.pattern.indexOf('\'') == -1) { - formattedDate = patternReplace(this.pattern); - } - - /* - * If the pattern contains one or more quoted strings, split it into pieces and parse only the - * non-quoted pattern letters. - */ - else { - var formatPatternParts = this.pattern.replace(/\'+(?=\')/g, '\'\'').split('\'\''); - if(formatPatternParts.length == 1) { - formatPatternParts = this.pattern.split('\''); - for(var i = 0; i < formatPatternParts.length; i++) { - if(i % 2 == 0) { - formatPatternParts[i] = patternReplace(formatPatternParts[i]); - } - } - return formatPatternParts.join(''); - } - else { - for(var i = 0; i < formatPatternParts.length; i++) { - var formatPatternParts2 = formatPatternParts[i].split('\''); - for(var j = 0; j < formatPatternParts2.length; j++) { - if(j % 2 == 0) { - formatPatternParts2[j] = patternReplace(formatPatternParts2[j]); - } - } - formatPatternParts[i] = formatPatternParts2.join(''); - } - return formatPatternParts.join('\''); - } - } - - return formattedDate; - }; -} \ No newline at end of file +} diff --git a/test/spec/SimpleDateFormatTest.js b/test/spec/SimpleDateFormatTest.js index 6799879..ad80f2c 100644 --- a/test/spec/SimpleDateFormatTest.js +++ b/test/spec/SimpleDateFormatTest.js @@ -348,7 +348,7 @@ describe('SimpleDateFormat', function() { sdf = new simpleDateFormat('EEEE', 'es_US'); var esUSWednesday = new Date(esUSDateClone.valueOf()); esUSWednesday.setDate(21); - expect(sdf.format(esUSWednesday)).toBe('miércoles'); + expect(sdf.format(esUSWednesday)).toBe('miércoles'); }); it('should get Thursday correct', function() { @@ -369,7 +369,7 @@ describe('SimpleDateFormat', function() { sdf = new simpleDateFormat('EEEE', 'es_US'); var esUSSaturday = new Date(esUSDateClone.valueOf()); esUSSaturday.setDate(24); - expect(sdf.format(esUSSaturday)).toBe('sábado'); + expect(sdf.format(esUSSaturday)).toBe('sábado'); }); }); }); @@ -388,7 +388,7 @@ describe('SimpleDateFormat', function() { sdf = new simpleDateFormat('MMMM', 'fr_CA'); var frCAFeb = new Date(frCADateClone.valueOf()); frCAFeb.setMonth(1); - expect(sdf.format(frCAFeb)).toBe('février'); + expect(sdf.format(frCAFeb)).toBe('février'); }); it('should get March correct', function() { @@ -430,7 +430,7 @@ describe('SimpleDateFormat', function() { sdf = new simpleDateFormat('MMMM', 'fr_CA'); var frCAAugust = new Date(frCADateClone.valueOf()); frCAAugust.setMonth(7); - expect(sdf.format(frCAAugust)).toBe('août'); + expect(sdf.format(frCAAugust)).toBe('août'); }); it('should get September correct', function() { @@ -458,7 +458,7 @@ describe('SimpleDateFormat', function() { sdf = new simpleDateFormat('MMMM', 'fr_CA'); var frCADecember = new Date(frCADateClone.valueOf()); frCADecember.setMonth(11); - expect(sdf.format(frCADecember)).toBe('décembre'); + expect(sdf.format(frCADecember)).toBe('décembre'); }); }); @@ -515,4 +515,287 @@ describe('SimpleDateFormat', function() { }); }); }); -}); \ No newline at end of file + /* es_US language tests */ + describe('es_US', function() { + describe('Should use the correct month names', function() { + var esUSDateClone = sdfDate; + + it('should get January correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + expect(sdf.format(esUSDateClone)).toBe('enero'); + }); + + it('should get February correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + var esUSFeb = new Date(esUSDateClone.valueOf()); + esUSFeb.setMonth(1); + expect(sdf.format(esUSFeb)).toBe('febrero'); + }); + + it('should get March correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + var esUSMarch = new Date(esUSDateClone.valueOf()); + esUSMarch.setMonth(2); + expect(sdf.format(esUSMarch)).toBe('marzo'); + }); + + it('should get April correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + var esUSApril = new Date(esUSDateClone.valueOf()); + esUSApril.setMonth(3); + expect(sdf.format(esUSApril)).toBe('abril'); + }); + + it('should get May correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + var esUSMay = new Date(esUSDateClone.valueOf()); + esUSMay.setMonth(4); + expect(sdf.format(esUSMay)).toBe('mayo'); + }); + + it('should get June correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + var esUSJune = new Date(esUSDateClone.valueOf()); + esUSJune.setMonth(5); + expect(sdf.format(esUSJune)).toBe('junio'); + }); + + it('should get July correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + var esUSJuly = new Date(esUSDateClone.valueOf()); + esUSJuly.setMonth(6); + expect(sdf.format(esUSJuly)).toBe('julio'); + }); + + it('should get August correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + var esUSAugust = new Date(esUSDateClone.valueOf()); + esUSAugust.setMonth(7); + expect(sdf.format(esUSAugust)).toBe('agosto'); + }); + + it('should get September correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + var esUSSeptember = new Date(esUSDateClone.valueOf()); + esUSSeptember.setMonth(8); + expect(sdf.format(esUSSeptember)).toBe('septiembre'); + }); + + it('should get October correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + var esUSOctober = new Date(esUSDateClone.valueOf()); + esUSOctober.setMonth(9); + expect(sdf.format(esUSOctober)).toBe('octubre'); + }); + + it('should get November correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + var esUSNovember = new Date(esUSDateClone.valueOf()); + esUSNovember.setMonth(10); + expect(sdf.format(esUSNovember)).toBe('noviembre'); + }); + + it('should get December correct', function() { + sdf = new simpleDateFormat('MMMM', 'es_US'); + var esUSDecember = new Date(esUSDateClone.valueOf()); + esUSDecember.setMonth(11); + expect(sdf.format(esUSDecember)).toBe('diciembre'); + }); + }); + + describe('Should use the correct day names', function() { + var esUSDateClone = sdfDate; + + it('should get Sunday correct', function() { + sdf = new simpleDateFormat('EEEE', 'es_US'); + var esUSSunday = new Date(esUSDateClone.valueOf()); + esUSSunday.setDate(18); + expect(sdf.format(esUSSunday)).toBe('domingo'); + }); + + it('should get Monday correct', function() { + sdf = new simpleDateFormat('EEEE', 'es_US'); + var esUSMonday = new Date(esUSDateClone.valueOf()); + esUSMonday.setDate(19); + expect(sdf.format(esUSMonday)).toBe('lunes'); + }); + + it('should get Tuesday correct', function() { + sdf = new simpleDateFormat('EEEE', 'es_US'); + var esUSTuesday = new Date(esUSDateClone.valueOf()); + esUSTuesday.setDate(20); + expect(sdf.format(esUSTuesday)).toBe('martes'); + }); + + it('should get Wednesday correct', function() { + sdf = new simpleDateFormat('EEEE', 'es_US'); + var esUSWednesday = new Date(esUSDateClone.valueOf()); + esUSWednesday.setDate(21); + expect(sdf.format(esUSWednesday)).toBe('miércoles'); + }); + + it('should get Thursday correct', function() { + sdf = new simpleDateFormat('EEEE', 'es_US'); + var esUSThursday = new Date(esUSDateClone.valueOf()); + esUSThursday.setDate(22); + expect(sdf.format(esUSThursday)).toBe('jueves'); + }); + + it('should get Friday correct', function() { + sdf = new simpleDateFormat('EEEE', 'es_US'); + var esUSFriday = new Date(esUSDateClone.valueOf()); + esUSFriday.setDate(23); + expect(sdf.format(esUSFriday)).toBe('viernes'); + }); + + it('should get Saturday correct', function() { + sdf = new simpleDateFormat('EEEE', 'es_US'); + var esUSSaturday = new Date(esUSDateClone.valueOf()); + esUSSaturday.setDate(24); + expect(sdf.format(esUSSaturday)).toBe('sábado'); + }); + }); + }); + + /* de_DE language tests */ + describe('de_DE', function() { + describe('Should use the correct month names', function() { + var deDEDateClone = sdfDate; + + it('should get January correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + expect(sdf.format(deDEDateClone)).toBe('Januar'); + }); + + it('should get February correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + var deDEFeb = new Date(deDEDateClone.valueOf()); + deDEFeb.setMonth(1); + expect(sdf.format(deDEFeb)).toBe('Februar'); + }); + + it('should get March correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + var deDEMarch = new Date(deDEDateClone.valueOf()); + deDEMarch.setMonth(2); + expect(sdf.format(deDEMarch)).toBe('März'); + }); + + it('should get April correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + var deDEApril = new Date(deDEDateClone.valueOf()); + deDEApril.setMonth(3); + expect(sdf.format(deDEApril)).toBe('April'); + }); + + it('should get May correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + var deDEMay = new Date(deDEDateClone.valueOf()); + deDEMay.setMonth(4); + expect(sdf.format(deDEMay)).toBe('Mai'); + }); + + it('should get June correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + var deDEJune = new Date(deDEDateClone.valueOf()); + deDEJune.setMonth(5); + expect(sdf.format(deDEJune)).toBe('Juni'); + }); + + it('should get July correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + var deDEJuly = new Date(deDEDateClone.valueOf()); + deDEJuly.setMonth(6); + expect(sdf.format(deDEJuly)).toBe('Juli'); + }); + + it('should get August correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + var deDEAugust = new Date(deDEDateClone.valueOf()); + deDEAugust.setMonth(7); + expect(sdf.format(deDEAugust)).toBe('August'); + }); + + it('should get September correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + var deDESeptember = new Date(deDEDateClone.valueOf()); + deDESeptember.setMonth(8); + expect(sdf.format(deDESeptember)).toBe('September'); + }); + + it('should get October correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + var deDEOctober = new Date(deDEDateClone.valueOf()); + deDEOctober.setMonth(9); + expect(sdf.format(deDEOctober)).toBe('Oktober'); + }); + + it('should get November correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + var deDENovember = new Date(deDEDateClone.valueOf()); + deDENovember.setMonth(10); + expect(sdf.format(deDENovember)).toBe('November'); + }); + + it('should get December correct', function() { + sdf = new simpleDateFormat('MMMM', 'de_DE'); + var deDEDecember = new Date(deDEDateClone.valueOf()); + deDEDecember.setMonth(11); + expect(sdf.format(deDEDecember)).toBe('Dezember'); + }); + }); + + describe('Should use the correct day names', function() { + var deDEDateClone = sdfDate; + + it('should get Sunday correct', function() { + sdf = new simpleDateFormat('EEEE', 'de_DE'); + var deDESunday = new Date(deDEDateClone.valueOf()); + deDESunday.setDate(18); + expect(sdf.format(deDESunday)).toBe('Sonntag'); + }); + + it('should get Monday correct', function() { + sdf = new simpleDateFormat('EEEE', 'de_DE'); + var deDEMonday = new Date(deDEDateClone.valueOf()); + deDEMonday.setDate(19); + expect(sdf.format(deDEMonday)).toBe('Montag'); + }); + + it('should get Tuesday correct', function() { + sdf = new simpleDateFormat('EEEE', 'de_DE'); + var deDETuesday = new Date(deDEDateClone.valueOf()); + deDETuesday.setDate(20); + expect(sdf.format(deDETuesday)).toBe('Dienstag'); + }); + + it('should get Wednesday correct', function() { + sdf = new simpleDateFormat('EEEE', 'de_DE'); + var deDEWednesday = new Date(deDEDateClone.valueOf()); + deDEWednesday.setDate(21); + expect(sdf.format(deDEWednesday)).toBe('Mittwoch'); + }); + + it('should get Thursday correct', function() { + sdf = new simpleDateFormat('EEEE', 'de_DE'); + var deDEThursday = new Date(deDEDateClone.valueOf()); + deDEThursday.setDate(22); + expect(sdf.format(deDEThursday)).toBe('Donnerstag'); + }); + + it('should get Friday correct', function() { + sdf = new simpleDateFormat('EEEE', 'de_DE'); + var deDEFriday = new Date(deDEDateClone.valueOf()); + deDEFriday.setDate(23); + expect(sdf.format(deDEFriday)).toBe('Freitag'); + }); + + it('should get Saturday correct', function() { + sdf = new simpleDateFormat('EEEE', 'de_DE'); + var deDESaturday = new Date(deDEDateClone.valueOf()); + deDESaturday.setDate(24); + expect(sdf.format(deDESaturday)).toBe('Samstag'); + }); + }); + }); +});