]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
iso week days should be in the 1-7 range, not 0-6. #699 http://en.wikipedia.org/wiki...
authorTim Wood <washwithcare@gmail.com>
Mon, 17 Jun 2013 20:48:01 +0000 (13:48 -0700)
committerTim Wood <washwithcare@gmail.com>
Mon, 17 Jun 2013 20:48:01 +0000 (13:48 -0700)
moment.js
test/moment/format.js
test/moment/weekday.js

index f6176b6de70e1a47b565ebd82671847db4aeb51e..3a2868f3e5ec3856e84cefaf60fe5437971500e2 100644 (file)
--- a/moment.js
+++ b/moment.js
         },
 
         isoWeekday : function (input) {
-            // iso weeks start on monday, which is 1, so we subtract 1 (and add
-            // 7 for negative mod to work).
-            var weekday = (this._d.getDay() + 6) % 7;
-            return input == null ? weekday : this.add("d", input - weekday);
+            // behaves the same as moment#day except
+            // as a getter, returns 7 instead of 0 (1-7 range instead of 0-6)
+            // as a setter, sunday should belong to the previous week.
+            return input == null ? this.day() || 7 : this.day(this.day() % 7 ? input : input - 7);
         },
 
         // If passed a language key, it will set the language for this
index ddfe34ff191aad9997fb3c81832e78a1dab214b7..69702123ec59a8540e8367d6ab0e88d67f6d17d8 100644 (file)
@@ -260,13 +260,13 @@ exports.format = {
     "iso weekday formats" : function(test) {
         test.expect(7);
 
-        test.equal(moment([1985, 1,  4]).format('E'), '0', "Feb  4 1985 is Monday    -- 0th day");
-        test.equal(moment([2029, 8, 18]).format('E'), '1', "Sep 18 2029 is Tuesday   -- 1st day");
-        test.equal(moment([2013, 3, 24]).format('E'), '2', "Apr 24 2013 is Wednesday -- 2nd day");
-        test.equal(moment([2015, 2,  5]).format('E'), '3', "Mar  5 2015 is Thursday  -- 3nd day");
-        test.equal(moment([1970, 0,  2]).format('E'), '4', "Jan  2 1970 is Friday    -- 4th day");
-        test.equal(moment([2001, 4, 12]).format('E'), '5', "May 12 2001 is Saturday  -- 5th day");
-        test.equal(moment([2000, 0,  2]).format('E'), '6', "Jan  2 2000 is Sunday    -- 6th day");
+        test.equal(moment([1985, 1,  4]).format('E'), '1', "Feb  4 1985 is Monday    -- 1st day");
+        test.equal(moment([2029, 8, 18]).format('E'), '2', "Sep 18 2029 is Tuesday   -- 2nd day");
+        test.equal(moment([2013, 3, 24]).format('E'), '3', "Apr 24 2013 is Wednesday -- 3rd day");
+        test.equal(moment([2015, 2,  5]).format('E'), '4', "Mar  5 2015 is Thursday  -- 4th day");
+        test.equal(moment([1970, 0,  2]).format('E'), '5', "Jan  2 1970 is Friday    -- 5th day");
+        test.equal(moment([2001, 4, 12]).format('E'), '6', "May 12 2001 is Saturday  -- 6th day");
+        test.equal(moment([2000, 0,  2]).format('E'), '7', "Jan  2 2000 is Sunday    -- 7th day");
 
         test.done();
     },
@@ -283,7 +283,7 @@ exports.format = {
         test.equal(moment([2001, 4, 14]).format('e'), '5', "May 14 2001 is Monday    -- 5th day");
         test.equal(moment([2000, 0,  4]).format('e'), '6', "Jan  4 2000 is Tuesday   -- 6th day");
 
-        test.done()
+        test.done();
     },
 
     "toString is just human readable format" : function(test) {
index acf1b02192dc355b8e4515a0d6cec1bf255e4297..ef1571e341b9bc04389f97fc3b608748b8d1df18 100644 (file)
@@ -7,17 +7,56 @@ exports.week_year = {
 
         for (i = 0; i < 7; ++i) {
             moment.lang('dow:' + i + ',doy:6', {week: {dow: i, doy: 6}});
-            test.equal(moment([1985, 1,  4]).isoWeekday(), 0, "Feb  4 1985 is Monday    -- 0th day");
-            test.equal(moment([2029, 8, 18]).isoWeekday(), 1, "Sep 18 2029 is Tuesday   -- 1st day");
-            test.equal(moment([2013, 3, 24]).isoWeekday(), 2, "Apr 24 2013 is Wednesday -- 2nd day");
-            test.equal(moment([2015, 2,  5]).isoWeekday(), 3, "Mar  5 2015 is Thursday  -- 3nd day");
-            test.equal(moment([1970, 0,  2]).isoWeekday(), 4, "Jan  2 1970 is Friday    -- 4th day");
-            test.equal(moment([2001, 4, 12]).isoWeekday(), 5, "May 12 2001 is Saturday  -- 5th day");
-            test.equal(moment([2000, 0,  2]).isoWeekday(), 6, "Jan  2 2000 is Sunday    -- 6th day");
+            test.equal(moment([1985, 1,  4]).isoWeekday(), 1, "Feb  4 1985 is Monday    -- 1st day");
+            test.equal(moment([2029, 8, 18]).isoWeekday(), 2, "Sep 18 2029 is Tuesday   -- 2nd day");
+            test.equal(moment([2013, 3, 24]).isoWeekday(), 3, "Apr 24 2013 is Wednesday -- 3rd day");
+            test.equal(moment([2015, 2,  5]).isoWeekday(), 4, "Mar  5 2015 is Thursday  -- 4th day");
+            test.equal(moment([1970, 0,  2]).isoWeekday(), 5, "Jan  2 1970 is Friday    -- 5th day");
+            test.equal(moment([2001, 4, 12]).isoWeekday(), 6, "May 12 2001 is Saturday  -- 6th day");
+            test.equal(moment([2000, 0,  2]).isoWeekday(), 7, "Jan  2 2000 is Sunday    -- 7th day");
         }
         test.done();
     },
 
+    "iso weekday setter" : function(test) {
+        test.expect(27);
+
+        var a = moment([2011, 0, 10]);
+        test.equal(moment(a).isoWeekday(1).date(),  10, 'set from mon to mon');
+        test.equal(moment(a).isoWeekday(4).date(),  13, 'set from mon to thu');
+        test.equal(moment(a).isoWeekday(7).date(),  16, 'set from mon to sun');
+        test.equal(moment(a).isoWeekday(-6).date(),  3, 'set from mon to last mon');
+        test.equal(moment(a).isoWeekday(-3).date(),  6, 'set from mon to last thu');
+        test.equal(moment(a).isoWeekday(0).date(),   9, 'set from mon to last sun');
+        test.equal(moment(a).isoWeekday(8).date(),  17, 'set from mon to next mon');
+        test.equal(moment(a).isoWeekday(11).date(), 20, 'set from mon to next thu');
+        test.equal(moment(a).isoWeekday(14).date(), 23, 'set from mon to next sun');
+
+        a = moment([2011, 0, 13]);
+        test.equal(moment(a).isoWeekday(1).date(), 10, 'set from thu to mon');
+        test.equal(moment(a).isoWeekday(4).date(), 13, 'set from thu to thu');
+        test.equal(moment(a).isoWeekday(7).date(), 16, 'set from thu to sun');
+        test.equal(moment(a).isoWeekday(-6).date(),  3, 'set from thu to last mon');
+        test.equal(moment(a).isoWeekday(-3).date(),  6, 'set from thu to last thu');
+        test.equal(moment(a).isoWeekday(0).date(),   9, 'set from thu to last sun');
+        test.equal(moment(a).isoWeekday(8).date(),  17, 'set from thu to next mon');
+        test.equal(moment(a).isoWeekday(11).date(), 20, 'set from thu to next thu');
+        test.equal(moment(a).isoWeekday(14).date(), 23, 'set from thu to next sun');
+
+        a = moment([2011, 0, 16]);
+        test.equal(moment(a).isoWeekday(1).date(), 10, 'set from sun to mon');
+        test.equal(moment(a).isoWeekday(4).date(), 13, 'set from sun to thu');
+        test.equal(moment(a).isoWeekday(7).date(), 16, 'set from sun to sun');
+        test.equal(moment(a).isoWeekday(-6).date(),  3, 'set from sun to last mon');
+        test.equal(moment(a).isoWeekday(-3).date(),  6, 'set from sun to last thu');
+        test.equal(moment(a).isoWeekday(0).date(),   9, 'set from sun to last sun');
+        test.equal(moment(a).isoWeekday(8).date(),  17, 'set from sun to next mon');
+        test.equal(moment(a).isoWeekday(11).date(), 20, 'set from sun to next thu');
+        test.equal(moment(a).isoWeekday(14).date(), 23, 'set from sun to next sun');
+
+        test.done();
+    },
+
     "weekday first day of week Sunday (dow 0)": function(test) {
         test.expect(7);