]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
adding a start/endOf week support 567/head
authorStuart <stuart.leigh83@gmail.com>
Tue, 8 Jan 2013 09:58:46 +0000 (09:58 +0000)
committerStuart <stuart.leigh83@gmail.com>
Wed, 23 Jan 2013 09:52:51 +0000 (09:52 +0000)
moment.js
test/moment/sod_eod.js

index b45633ba8ff37e83826966242175ecd6709fc940..de53c6c2a5c84df79bb956e6ef2199c51b7d51af 100644 (file)
--- a/moment.js
+++ b/moment.js
         },
 
         startOf: function (units) {
+            units = units.replace(/s$/, '');
             // the following switch intentionally omits break keywords
             // to utilize falling through the cases.
-            switch (units.replace(/s$/, '')) {
+            switch (units) {
             case 'year':
                 this.month(0);
                 /* falls through */
             case 'month':
                 this.date(1);
                 /* falls through */
+            case 'week':
             case 'day':
                 this.hours(0);
                 /* falls through */
                 this.milliseconds(0);
                 /* falls through */
             }
+
+            // weeks are a special case
+            if (units === 'week') {
+                this.day(0);
+            }
+
             return this;
         },
 
index fd3a856889e423096a2b4a3b8cddaa6cfca635f9..272eb2f556f7e5bc7832de650c84e520401217df 100644 (file)
@@ -101,6 +101,40 @@ exports.eod_sod = {
         test.equal(m.milliseconds(), 999, "set the seconds");
         test.done();
     },
+
+    "start of week" : function(test) {
+        test.expect(9);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('week');
+        var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('weeks');
+        test.equal(+m, +ms, "Plural or singular should work");
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 0, "rolls back to January");
+        test.equal(m.day(), 0, "set day of week");
+        test.equal(m.date(), 30, "set correct date");
+        test.equal(m.hours(), 0, "strip out the hours");
+        test.equal(m.minutes(), 0, "strip out the minutes");
+        test.equal(m.seconds(), 0, "strip out the seconds");
+        test.equal(m.milliseconds(), 0, "strip out the milliseconds");
+        test.done();
+    },
+    
+    "end of week" : function(test) {
+        test.expect(9);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('week');
+        var ms = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('weeks');
+        test.equal(+m, +ms, "Plural or singular should work");
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 1, "keep the month");
+        test.equal(m.day(), 6, "set the day of the week");
+        test.equal(m.date(), 5, "set the day");
+        test.equal(m.hours(), 23, "set the hours");
+        test.equal(m.minutes(), 59, "set the minutes");
+        test.equal(m.seconds(), 59, "set the seconds");
+        test.equal(m.milliseconds(), 999, "set the seconds");
+        test.done();
+    },
     
     "start of day" : function(test) {
         test.expect(8);