From: Isaac Cambron Date: Sat, 22 Mar 2014 21:21:07 +0000 (-0400) Subject: allowing customized year parsing X-Git-Tag: 2.6.0~16^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6fc7347464ec0177ff59f022bd4e68480692b7fa;p=thirdparty%2Fmoment.git allowing customized year parsing --- diff --git a/moment.js b/moment.js index 0018faebf..41a241ec4 100644 --- a/moment.js +++ b/moment.js @@ -1086,7 +1086,7 @@ break; // YEAR case 'YY' : - datePartArray[YEAR] = toInt(input) + (toInt(input) > 68 ? 1900 : 2000); + datePartArray[YEAR] = moment.parseTwoDigitYear(input); break; case 'YYYY' : case 'YYYYY' : @@ -1771,6 +1771,10 @@ return moment.apply(null, arguments).parseZone(); }; + moment.parseTwoDigitYear = function(input) { + return toInt(input) + (toInt(input) > 68 ? 1900 : 2000); + } + /************************************ Moment Prototype ************************************/ diff --git a/test/moment/create.js b/test/moment/create.js index c0a3a606c..70e36dae3 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -811,5 +811,27 @@ exports.create = { moment.lang('en'); test.done(); } + }, + + 'parsing with customized two-digit year' : function (test) { + var original = moment.parseTwoDigitYear; + try { + test.equal(moment('68', 'YY').year(), 2068); + test.equal(moment('69', 'YY').year(), 1969); + moment.parseTwoDigitYear = function(input){ + return +input + (+input > 30 ? 1900 : 2000); + }; + test.equal(moment('68', 'YY').year(), 1968); + test.equal(moment('67', 'YY').year(), 1967); + test.equal(moment('31', 'YY').year(), 1931); + test.equal(moment('30', 'YY').year(), 2030); + + } + finally { + moment.parseTwoDigitYear = original; + test.done(); + } + } + };