]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Add format and parse token Y, so it actually works
authorIskren Chernev <iskren.chernev@gmail.com>
Wed, 6 Jan 2016 22:24:28 +0000 (00:24 +0200)
committerIskren Chernev <iskren.chernev@gmail.com>
Sat, 9 Jan 2016 09:36:35 +0000 (11:36 +0200)
src/lib/units/year.js
src/test/moment/create.js
src/test/moment/format.js

index cc32b55b65cf3023d89bb2bbbd1e0938eee78c6e..5244e0c0ea0ac09fa28c1cf40adf1dd74e5f24c8 100644 (file)
@@ -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
 
index dbe31af082987b014e5286b0cb8ae07ca2a1eb3b..02181e40b668c76e2e6c7d5f2499dd96fe046ae4 100644 (file)
@@ -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');
+});
index 017145f4ac878265d0af184e6aa635632d85b1c4..f20cab2a0253f70be0fb3d0cd9108f425df27345 100644 (file)
@@ -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');
+});