]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix JavaScript setMonth() behavior 822/head
authorJustin Warkentin <jwarkentin@deseretdigital.com>
Fri, 31 May 2013 20:14:01 +0000 (14:14 -0600)
committerJustin Warkentin <jwarkentin@deseretdigital.com>
Sun, 23 Jun 2013 05:54:27 +0000 (23:54 -0600)
moment.js
test/moment/getters_setters.js

index 2579952556b198d99a532ebcde65c222930ba8ea..a8425c7d1dcfaba8479e725719d7aa86cf51ffee 100644 (file)
--- a/moment.js
+++ b/moment.js
             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);
         },
 
         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);
                         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 {
index f8e0a0085d748bedddfe5667ab85db47887b10c8..2e79deb473345aa18fa23dd879b570e51053f5c2 100644 (file)
@@ -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();
     },