From c40ef7bd15d1bd38b4edfa8d39901ca349229ac4 Mon Sep 17 00:00:00 2001 From: Justin Warkentin Date: Fri, 31 May 2013 14:14:01 -0600 Subject: [PATCH] Fix JavaScript setMonth() behavior --- moment.js | 15 ++++++++++----- test/moment/getters_setters.js | 8 +++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/moment.js b/moment.js index 257995255..a8425c7d1 100644 --- a/moment.js +++ b/moment.js @@ -351,10 +351,7 @@ mom.date(mom.date() + days * isAdding); } if (months) { - currentDate = mom.date(); - mom.date(1) - .month(mom.month() + months * isAdding) - .date(Math.min(currentDate, mom.daysInMonth())); + mom.month(mom.month() + months * isAdding); } if (milliseconds && !ignoreUpdateOffset) { moment.updateOffset(mom); @@ -1297,7 +1294,10 @@ }, month : function (input) { - var utc = this._isUTC ? 'UTC' : ''; + var utc = this._isUTC ? 'UTC' : '', + dayOfMonth, + daysInMonth; + if (input != null) { if (typeof input === 'string') { input = this.lang().monthsParse(input); @@ -1305,7 +1305,12 @@ return this; } } + + dayOfMonth = this.date(); + this.date(1); this._d['set' + utc + 'Month'](input); + this.date(Math.min(dayOfMonth, this.daysInMonth())); + moment.updateOffset(this); return this; } else { diff --git a/test/moment/getters_setters.js b/test/moment/getters_setters.js index f8e0a0085..2e79deb47 100644 --- a/test/moment/getters_setters.js +++ b/test/moment/getters_setters.js @@ -61,7 +61,7 @@ exports.getters_setters = { }, "setters" : function(test) { - test.expect(8); + test.expect(9); var a = moment(); a.year(2011); @@ -79,6 +79,12 @@ exports.getters_setters = { test.equal(a.minutes(), 7, 'minute'); test.equal(a.seconds(), 8, 'second'); test.equal(a.milliseconds(), 9, 'milliseconds'); + + // Test month() behavior. See https://github.com/timrwood/moment/pull/822 + a = moment('20130531', 'YYYYMMDD'); + a.month(3); + test.equal(a.month(), 3, 'month edge case'); + test.done(); }, -- 2.47.2