Skip to content

Commit

Permalink
Merge pull request #1545 from icambron/abstracted-year-parsing
Browse files Browse the repository at this point in the history
Allow customized two-digit-year parsing
  • Loading branch information
ichernev committed Apr 2, 2014
2 parents ba96963 + f781846 commit b2020bf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' :
Expand Down Expand Up @@ -1782,6 +1782,10 @@
return moment.apply(null, arguments).parseZone();
};

moment.parseTwoDigitYear = function (input) {
return toInt(input) + (toInt(input) > 68 ? 1900 : 2000);
};

/************************************
Moment Prototype
************************************/
Expand Down
19 changes: 19 additions & 0 deletions test/moment/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,5 +824,24 @@ 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();
}
}
};

0 comments on commit b2020bf

Please sign in to comment.