Skip to content

Commit

Permalink
Merge pull request #822 from jwarkentin/develop
Browse files Browse the repository at this point in the history
Fix JavaScript setMonth() behavior
  • Loading branch information
ichernev committed Jun 24, 2013
2 parents fbba084 + c40ef7b commit fdc9881
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 10 additions & 5 deletions moment.js
Expand Up @@ -348,10 +348,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);
Expand Down Expand Up @@ -1319,15 +1316,23 @@
},

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);
if (typeof input !== 'number') {
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 {
Expand Down
8 changes: 7 additions & 1 deletion test/moment/getters_setters.js
Expand Up @@ -61,7 +61,7 @@ exports.getters_setters = {
},

"setters" : function(test) {
test.expect(8);
test.expect(9);

var a = moment();
a.year(2011);
Expand All @@ -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();
},

Expand Down

0 comments on commit fdc9881

Please sign in to comment.