]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix year setter for random days in leap year (#4255)
authorYann Pringault <yann.pringault@gmail.com>
Sat, 4 Nov 2017 14:37:49 +0000 (15:37 +0100)
committerMaggie Pint <maggiepint@gmail.com>
Sat, 4 Nov 2017 14:37:49 +0000 (07:37 -0700)
src/lib/moment/get-set.js
src/test/moment/getters_setters.js

index ce5ac823d31da4813806501bbe7dbd6bc713bdc6..f5035f1a4b75d1f8c129d28b1312c80768f3d651 100644 (file)
@@ -24,7 +24,7 @@ export function get (mom, unit) {
 
 export function set (mom, unit, value) {
     if (mom.isValid() && !isNaN(value)) {
-        if (unit === 'FullYear' && isLeapYear(mom.year())) {
+        if (unit === 'FullYear' && isLeapYear(mom.year()) && mom.month() === 1 && mom.date() === 29) {
             mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value, mom.month(), daysInMonth(value, mom.month()));
         }
         else {
index 280ee6ea060e302628bcfee9f0fe7207ef5f2051..2c47a8913ecbee6a91c65b10579e1afe3a000fcc 100644 (file)
@@ -277,6 +277,10 @@ test('year setter', function (assert) {
     var b = moment([2012, 1, 29]);
     assert.equal(moment(b).year(2017).format('YYYY-MM-DD'), '2017-02-28', 'set from last day of february on a leap year to a non leap year');
     assert.equal(moment(b).year(2004).format('YYYY-MM-DD'), '2004-02-29', 'set from last day of february on a leap year to a leap year');
+
+    var c = moment([2012, 9, 4]);
+    assert.equal(moment(c).year(2017).format('YYYY-MM-DD'), '2017-10-04', 'set from a random day on a leap year to a non leap year');
+    assert.equal(moment(c).year(2004).format('YYYY-MM-DD'), '2004-10-04', 'set from a random day on a leap year to a leap year');
 });
 
 test('object set ordering', function (assert) {