]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix two digit year parsing with YYYY format
authorStewart MacKenzie-Leigh <stewart@stugo.co.uk>
Mon, 20 Jul 2015 13:47:03 +0000 (14:47 +0100)
committerIskren Chernev <iskren.chernev@gmail.com>
Sun, 26 Jul 2015 04:48:36 +0000 (21:48 -0700)
Two digit years will use the parseTwoDigitYear function when parsing with the YYYY format.
Closes #2343

src/lib/units/year.js
src/test/moment/create.js

index 4d8a03b148be75010c0e3839e89705b3eba16547..cc32b55b65cf3023d89bb2bbbd1e0938eee78c6e 100644 (file)
@@ -29,7 +29,10 @@ addRegexToken('YYYY',   match1to4, match4);
 addRegexToken('YYYYY',  match1to6, match6);
 addRegexToken('YYYYYY', match1to6, match6);
 
-addParseToken(['YYYY', 'YYYYY', 'YYYYYY'], YEAR);
+addParseToken(['YYYYY', 'YYYYYY'], YEAR);
+addParseToken('YYYY', function (input, array) {
+    array[YEAR] = input.length === 2 ? hooks.parseTwoDigitYear(input) : toInt(input);
+});
 addParseToken('YY', function (input, array) {
     array[YEAR] = hooks.parseTwoDigitYear(input);
 });
@@ -57,4 +60,3 @@ export var getSetYear = makeGetSet('FullYear', false);
 export function getIsLeapYear () {
     return isLeapYear(this.year());
 }
-
index 0c33104d8c8d3f8a151ac34b70c8b34eb531fd8c..0de775210d66218e0348fb46775a55485bd1ce5c 100644 (file)
@@ -240,6 +240,13 @@ test('string with format', function (assert) {
     }
 });
 
+test('2 digit year with YYYY format', function (assert) {
+    assert.equal(moment('9/2/99', 'D/M/YYYY').format('DD/MM/YYYY'), '09/02/1999', 'D/M/YYYY ---> 9/2/99');
+    assert.equal(moment('9/2/1999', 'D/M/YYYY').format('DD/MM/YYYY'), '09/02/1999', 'D/M/YYYY ---> 9/2/1999');
+    assert.equal(moment('9/2/68', 'D/M/YYYY').format('DD/MM/YYYY'), '09/02/2068', 'D/M/YYYY ---> 9/2/68');
+    assert.equal(moment('9/2/69', 'D/M/YYYY').format('DD/MM/YYYY'), '09/02/1969', 'D/M/YYYY ---> 9/2/69');
+});
+
 test('unix timestamp format', function (assert) {
     var formats = ['X', 'X.S', 'X.SS', 'X.SSS'], i, format;
 
@@ -637,7 +644,6 @@ test('first century', function (assert) {
     assert.equal(moment([99, 0, 1]).format('YYYY-MM-DD'), '0099-01-01', 'Year AD 99');
     assert.equal(moment([999, 0, 1]).format('YYYY-MM-DD'), '0999-01-01', 'Year AD 999');
     assert.equal(moment('0 1 1', 'YYYY MM DD').format('YYYY-MM-DD'), '0000-01-01', 'Year AD 0');
-    assert.equal(moment('99 1 1', 'YYYY MM DD').format('YYYY-MM-DD'), '0099-01-01', 'Year AD 99');
     assert.equal(moment('999 1 1', 'YYYY MM DD').format('YYYY-MM-DD'), '0999-01-01', 'Year AD 999');
     assert.equal(moment('0 1 1', 'YYYYY MM DD').format('YYYYY-MM-DD'), '00000-01-01', 'Year AD 0');
     assert.equal(moment('99 1 1', 'YYYYY MM DD').format('YYYYY-MM-DD'), '00099-01-01', 'Year AD 99');