]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Added generic functions for end of and start of for years, months, days, hours, minut...
authorMattias Runge <mattias@runge.se>
Mon, 23 Apr 2012 18:19:39 +0000 (20:19 +0200)
committerMattias Runge <mattias@runge.se>
Mon, 23 Apr 2012 18:19:39 +0000 (20:19 +0200)
moment.js
test/moment/sod_eod.js

index adea93514436b0820aa172a72886929cfa762627..d14cbbb671e7b6f4759b1a4f2c7579d3c527385d 100644 (file)
--- a/moment.js
+++ b/moment.js
                 this.add({ d : input - day });
         },
 
+        startOf: function (val) {
+            if (val === 'years') {
+                return this.clone()
+                    .month(0)
+                    .date(1)
+                    .hours(0)
+                    .minutes(0)
+                    .seconds(0)
+                    .milliseconds(0);
+            } else if (val === 'months') {
+                return this.clone()
+                    .date(1)
+                    .hours(0)
+                    .minutes(0)
+                    .seconds(0)
+                    .milliseconds(0);
+            } else if (val === 'days') {
+                return this.clone()
+                    .hours(0)
+                    .minutes(0)
+                    .seconds(0)
+                    .milliseconds(0);
+            } else if (val === 'hours') {
+                return this.clone()
+                    .minutes(0)
+                    .seconds(0)
+                    .milliseconds(0);
+            } else if (val === 'minutes') {
+                return this.clone()
+                    .seconds(0)
+                    .milliseconds(0);
+            } else if (val === 'seconds') {
+                return this.clone()
+                    .milliseconds(0);
+            }
+            return this.clone();
+        },
+        
+        endOf: function (val) {
+            return this.startOf(val).add(val, 1).subtract('milliseconds', 1);
+        },
+        
         sod: function () {
-            return this.clone()
-                .hours(0)
-                .minutes(0)
-                .seconds(0)
-                .milliseconds(0);
+            return this.startOf('days');
         },
 
         eod: function () {
             // end of day = start of day plus 1 day, minus 1 millisecond
-            return this.sod().add({
-                d : 1,
-                ms : -1
-            });
+            return this.endOf('days');
         },
 
         zone : function () {
index 8e2cebff13afdfebdaa295c3a96a6f753f32a5c8..669d2851bbf3725730cff81a79859189e152a703 100644 (file)
@@ -36,5 +36,173 @@ exports.eod_sod = {
         test.equal(m2.eod().valueOf(), m2.hours(23).minutes(59).seconds(59).milliseconds(999).valueOf(), "Eod should equal manual hours/mins/seconds");
         
         test.done();
-    }
+    },
+    
+    "start of year" : function(test) {
+        test.expect(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('years');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 0, "strip out the month");
+        test.equal(m.date(), 1, "strip out the day");
+        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 year" : function(test) {
+        test.expect(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('years');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 11, "set the month");
+        test.equal(m.date(), 31, "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 month" : function(test) {
+        test.expect(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('months');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 1, "keep the month");
+        test.equal(m.date(), 1, "strip out the day");
+        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 month" : function(test) {
+        test.expect(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('months');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 1, "keep the month");
+        test.equal(m.date(), 28, "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(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('days');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 1, "keep the month");
+        test.equal(m.date(), 2, "keep the day");
+        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 day" : function(test) {
+        test.expect(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('days');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 1, "keep the month");
+        test.equal(m.date(), 2, "keep 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 hour" : function(test) {
+        test.expect(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('hours');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 1, "keep the month");
+        test.equal(m.date(), 2, "keep the day");
+        test.equal(m.hours(), 3, "keep 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 hour" : function(test) {
+        test.expect(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('hours');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 1, "keep the month");
+        test.equal(m.date(), 2, "keep the day");
+        test.equal(m.hours(), 3, "keep 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 minute" : function(test) {
+        test.expect(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('minutes');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 1, "keep the month");
+        test.equal(m.date(), 2, "keep the day");
+        test.equal(m.hours(), 3, "keep the hours"); 
+        test.equal(m.minutes(), 4, "keep the minutes"); 
+        test.equal(m.seconds(), 0, "strip out the seconds"); 
+        test.equal(m.milliseconds(), 0, "strip out the milliseconds");
+        test.done();
+    },
+    
+    "end of minute" : function(test) {
+        test.expect(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('minutes');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 1, "keep the month");
+        test.equal(m.date(), 2, "keep the day");
+        test.equal(m.hours(), 3, "keep the hours"); 
+        test.equal(m.minutes(), 4, "keep the minutes"); 
+        test.equal(m.seconds(), 59, "set the seconds"); 
+        test.equal(m.milliseconds(), 999, "set the seconds");
+        test.done();
+    },
+    
+    "start of second" : function(test) {
+        test.expect(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).startOf('seconds');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 1, "keep the month");
+        test.equal(m.date(), 2, "keep the day");
+        test.equal(m.hours(), 3, "keep the hours"); 
+        test.equal(m.minutes(), 4, "keep the minutes"); 
+        test.equal(m.seconds(), 5, "keep the the seconds"); 
+        test.equal(m.milliseconds(), 0, "strip out the milliseconds");
+        test.done();
+    },
+    
+    "end of second" : function(test) {
+        test.expect(7);
+
+        var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)).endOf('seconds');
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.month(), 1, "keep the month");
+        test.equal(m.date(), 2, "keep the day");
+        test.equal(m.hours(), 3, "keep the hours"); 
+        test.equal(m.minutes(), 4, "keep the minutes"); 
+        test.equal(m.seconds(), 5, "keep the seconds"); 
+        test.equal(m.milliseconds(), 999, "set the seconds");
+        test.done();
+    },
 };