]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Setting year from feb 29 on leap year to a non leap year returned mar 1.
authorÍvar Oddsson <ivaroddsson@gmail.com>
Tue, 3 Oct 2017 01:04:39 +0000 (01:04 +0000)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 9 Oct 2017 23:01:02 +0000 (02:01 +0300)
src/lib/moment/get-set.js
src/test/moment/getters_setters.js

index d583b36d34ebcea6c38c2f3f489efba2697d2337..ce5ac823d31da4813806501bbe7dbd6bc713bdc6 100644 (file)
@@ -2,6 +2,8 @@ import { normalizeUnits, normalizeObjectUnits } from '../units/aliases';
 import { getPrioritizedUnits } from '../units/priorities';
 import { hooks } from '../utils/hooks';
 import isFunction from '../utils/is-function';
+import { daysInMonth } from '../units/month';
+import { isLeapYear } from '../units/year';
 
 export function makeGetSet (unit, keepTime) {
     return function (value) {
@@ -22,7 +24,12 @@ export function get (mom, unit) {
 
 export function set (mom, unit, value) {
     if (mom.isValid() && !isNaN(value)) {
-        mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value);
+        if (unit === 'FullYear' && isLeapYear(mom.year())) {
+            mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value, mom.month(), daysInMonth(value, mom.month()));
+        }
+        else {
+            mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value);
+        }
     }
 }
 
index 87bcba60b3df75a6837f70950481f625f956977c..280ee6ea060e302628bcfee9f0fe7207ef5f2051 100644 (file)
@@ -269,6 +269,16 @@ test('day setter', function (assert) {
     assert.equal(moment(a).day(17).date(), 26, 'set from wednesday to second next wednesday');
 });
 
+test('year setter', function (assert) {
+    var a = moment([2015, 3, 15]);
+    assert.equal(moment(a).year(2016).format('YYYY-MM-DD'), '2016-04-15', 'set from 2015 to 2016');
+    assert.equal(moment(a).year(2011).format('YYYY-MM-DD'), '2011-04-15', 'set from 2015 to 2011');
+
+    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');
+});
+
 test('object set ordering', function (assert) {
     var a = moment([2016,3,30]);
     assert.equal(a.set({date:31, month:4}).date(), 31, 'setter order automatically arranged by size');