]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[bugfix] Force four digits in Y token (#3846)
authorKunal Marwaha <marwahaha@berkeley.edu>
Sat, 25 Apr 2020 20:50:48 +0000 (13:50 -0700)
committerGitHub <noreply@github.com>
Sat, 25 Apr 2020 20:50:48 +0000 (23:50 +0300)
src/lib/units/year.js
src/test/moment/format.js

index ccbad583e519cfe94467c7357425431b6528b839..3384aefad4512dcc29408a3cb73ba9baecbe1024 100644 (file)
@@ -8,12 +8,13 @@ import { isLeapYear } from '../utils/is-leap-year';
 import { hooks } from '../utils/hooks';
 import { YEAR } from './constants';
 import toInt from '../utils/to-int';
+import zeroFill from '../utils/zero-fill';
 
 // FORMATTING
 
 addFormatToken('Y', 0, 0, function () {
     var y = this.year();
-    return y <= 9999 ? '' + y : '+' + y;
+    return y <= 9999 ? zeroFill(y, 4) : '+' + y;
 });
 
 addFormatToken(0, ['YY', 2], 0, function () {
index c3ca663d95a0dc90800f008243465ce245f0020e..cb5b8c5a7aef894ffcb89bd064439a4310b30b77 100644 (file)
@@ -517,10 +517,10 @@ test('k and kk', function (assert) {
 
 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('-123-01-01', 'Y-MM-DD', true).format('Y'), '-0123', 'format -123 with Y');
     assert.equal(moment('12345-01-01', 'Y-MM-DD', true).format('Y'), '+12345', 'format 12345 with Y');
-    assert.equal(moment('0-01-01', 'Y-MM-DD', true).format('Y'), '0', 'format 0 with Y');
-    assert.equal(moment('1-01-01', 'Y-MM-DD', true).format('Y'), '1', 'format 1 with Y');
+    assert.equal(moment('0-01-01', 'Y-MM-DD', true).format('Y'), '0000', 'format 0 with Y');
+    assert.equal(moment('1-01-01', 'Y-MM-DD', true).format('Y'), '0001', 'format 1 with Y');
     assert.equal(moment('9999-01-01', 'Y-MM-DD', true).format('Y'), '9999', 'format 9999 with Y');
     assert.equal(moment('10000-01-01', 'Y-MM-DD', true).format('Y'), '+10000', 'format 10000 with Y');
 });