Skip to content

Commit

Permalink
Fix year setter for random days in leap year (#4255)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerumen authored and maggiepint committed Nov 4, 2017
1 parent 29afed6 commit 6beb383
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/moment/get-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/test/moment/getters_setters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6beb383

Please sign in to comment.