]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Added tests for date.add and date.subtract
authorTim Wood <washwithcare@gmail.com>
Tue, 8 Mar 2011 17:04:01 +0000 (09:04 -0800)
committerTim Wood <washwithcare@gmail.com>
Tue, 8 Mar 2011 17:04:01 +0000 (09:04 -0800)
lib/underscore.date.js
test/date.js

index 8d1670845585566423557e38538cee0763451b9a..cd64fbaddb6b1bcc0299db3664bcffd1a343d5bc 100644 (file)
     
     // helper function for Date.prototype.add and Date.prototype.subtract
     function dateAddRemove(input, self, adding){
-       var ms = input.ms || 0 +
-               input.s || 0 * 1000 +
-               input.m || 0 * 1000 * 60 +
-               input.h || 0 * 1000 * 60 * 60 +
-               input.d || 0 * 1000 * 60 * 60 * 24 +
-               input.w || 0 * 1000 * 60 * 60 * 24 * 7,
+       var ms = (input.ms || 0) +
+               ((input.s || 0) * 1000) +
+               (input.m || 0) * 1000 * 60 +
+               (input.h || 0) * 1000 * 60 * 60 +
+               (input.d || 0) * 1000 * 60 * 60 * 24 +
+               (input.w || 0) * 1000 * 60 * 60 * 24 * 7,
                M = input.M,
                y = input.y;
         ms && self.setMilliseconds(self.getMilliseconds() + ms * adding); 
+        y && self.setFullYear(self.getFullYear() + y * adding);
         M && self.setMonth(self.getMonth() + M * adding); 
-        y && self.setYear(self.getYear() + y * adding);
     }
     
     dateProto.add = function (input) {
index 284c9ec8405ce0403b1b3471d5c0e3cc39d69baa..4b46ec9607e33a7511d8b4eab5f7d12e783326d2 100644 (file)
@@ -19,5 +19,23 @@ $(document).ready(function() {
        equal(dateTest.humanize("s ss"), "50 50");
        equal(dateTest.humanize("a A"), "pm PM");
   });
+  
+  test("add", function() {
+       var dateTest = new Date(2010, 1, 14, 15, 25, 50, 125);
+    expect(5);
+       equal(dateTest.humanize("MMMM Do YYYY, h:mm:ss a"), "February 14th 2010, 3:25:50 pm");
+    
+    dateTest.add({ms:200,s:10,m:10,h:2,d:3,M:2,y:3});
+       equal(dateTest.humanize("MMMM Do YYYY, h:mm:ss a"), "April 17th 2013, 5:36:00 pm");
+    
+    dateTest.add({w:1});
+    equal(dateTest.humanize("MMMM Do YYYY, h:mm:ss a"), "April 24th 2013, 5:36:00 pm");
+    
+       dateTest = new Date(2010, 0, 31);
+    equal(dateTest.humanize("MMMM Do YYYY"), "January 31st 2010");
+    
+    dateTest.add({M:1});
+    equal(dateTest.humanize("MMMM Do YYYY"), "February 28th 2010");
+  });
 
 });
\ No newline at end of file