From: Iskren Chernev Date: Wed, 6 Jan 2016 22:24:28 +0000 (+0200) Subject: Add format and parse token Y, so it actually works X-Git-Tag: 2.11.1~10^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d9cefb39c963bd85d55ae069deb9e904d9bbc87;p=thirdparty%2Fmoment.git Add format and parse token Y, so it actually works --- diff --git a/src/lib/units/year.js b/src/lib/units/year.js index cc32b55b6..5244e0c0e 100644 --- a/src/lib/units/year.js +++ b/src/lib/units/year.js @@ -9,6 +9,18 @@ import toInt from '../utils/to-int'; // FORMATTING +addFormatToken('Y', 0, 0, function () { + var y = this.year(); + if (y < 0) { + return y; + } else if (y <= 9999) { + return y; + } else { + // force plus for longer years. + return '+' + y; + } +}); + addFormatToken(0, ['YY', 2], 0, function () { return this.year() % 100; }); @@ -36,6 +48,9 @@ addParseToken('YYYY', function (input, array) { addParseToken('YY', function (input, array) { array[YEAR] = hooks.parseTwoDigitYear(input); }); +addParseToken('Y', function (input, array) { + array[YEAR] = parseInt(input, 10); +}); // HELPERS diff --git a/src/test/moment/create.js b/src/test/moment/create.js index dbe31af08..02181e40b 100644 --- a/src/test/moment/create.js +++ b/src/test/moment/create.js @@ -1037,3 +1037,7 @@ test('hmm', function (assert) { assert.equal(moment('112345', 'Hmmss', true).format('HH:mm:ss'), '11:23:45', '112345 with Hmmss'); assert.equal(moment('172345', 'Hmmss', true).format('HH:mm:ss'), '17:23:45', '112345 with Hmmss'); }); + +test('Y token', function (assert) { + assert.equal(moment('1-1-2010', 'M-D-Y', true).year(), 2010, 'parsing Y'); +}); diff --git a/src/test/moment/format.js b/src/test/moment/format.js index 017145f4a..f20cab2a0 100644 --- a/src/test/moment/format.js +++ b/src/test/moment/format.js @@ -424,3 +424,9 @@ test('Hmm and Hmmss', function (assert) { assert.equal(moment('08:34:56', 'HH:mm:ss').format('Hmmss'), '83456'); assert.equal(moment('18:34:56', 'HH:mm:ss').format('Hmmss'), '183456'); }); + +test('Y token', function (assert) { + assert.equal(moment('2010-01-01', 'YYYY-MM-DD', true).format('Y'), '2010', 'format 2010 with Y'); + assert.equal(moment('-123-01-01', 'Y-MM-DD', true).format('Y'), '-123', 'format -123 with Y'); + assert.equal(moment('12345-01-01', 'Y-MM-DD', true).format('Y'), '+12345', 'format 12345 with Y'); +});