]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
started adding Date.add and Date.subtract
authorTim Wood <washwithcare@gmail.com>
Tue, 8 Mar 2011 05:02:50 +0000 (21:02 -0800)
committerTim Wood <washwithcare@gmail.com>
Tue, 8 Mar 2011 05:02:50 +0000 (21:02 -0800)
lib/underscore.date.js

index 97371b33acaf216f7b0f87fe146b2bc54e915173..b5557f66077c7dca843d411c74d36eba5b97a906 100644 (file)
@@ -9,6 +9,8 @@
 
 
 (function(Date, _, undefined){
+       // create shortcuts to Date.prototype for minification
+       var dateProto = Date.prototype;
     // assign variables here so they can be overwritten for i18n or customization
     var self = this, _d,
         wordsMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
@@ -47,7 +49,7 @@
     }
     
     // Date.prototype.humanize
-    function humanize(inputString) {
+    dateProto.humanize = function(inputString) {
         // shortcuts to this and getting time functions
         // done to save bytes in minification
         var self = this,
         return inputString.replace(charactersToReplace, replaceFunction);
     }
     
+    // is leap year
+    dateProto.isleapyear = function() {
+       var year = this.getFullYear();
+       return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
+    }
+    
+    // is daylight savings time
+    dateProto.isdst = function() {
+       var year = this.getFullYear();
+               var offset = new Date(year, 0, 1).getTimezoneOffset();
+               offset = Math.max(offset, new Date(year, 6, 1).getTimezoneOffset());
+               return this.getTimezoneOffset() < offset;
+    }
+    
+    // helper function for Date.prototype.add and Date.prototype.subtract
+    function dateAddRemove(input, self, adding){
+       var ms = input.ms,
+               s = input.s,
+               m = input.m,
+               h = input.h,
+               d = input.d,
+               w = input.w,
+               M = input.M,
+               y = input.y;
+        ms && self.addMilliseconds(ms * adding); 
+        s && self.addSeconds(s * adding); 
+        m && self.addMinutes(m * adding); 
+        h && self.addHours(h * adding); 
+        d && self.addDays(d * adding); 
+        w && self.addWeeks(w * adding);
+        M && self.addMonths(M * adding);
+        y && self.addYears(y * adding);
+    }
+    
+    dateProto.add = function (input) {
+       dateAddRemove(input, this, 1);
+    };
+    
+    dateProto.subtract = function (input) {
+       dateAddRemove(input, this, -1);
+    };
+    
     /*
      * Underscore mixins
      */
         }
     };
     
-    // assign to prototype
-    Date.prototype.humanize = humanize;
-    
     // assign to global
     self._d = _d;