From: Tim Wood Date: Tue, 8 Mar 2011 17:04:01 +0000 (-0800) Subject: Added tests for date.add and date.subtract X-Git-Tag: 0.3.0~2^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8f1d6a4dd9f66eee63a51e5d63aa23cf7bc9a9a3;p=thirdparty%2Fmoment.git Added tests for date.add and date.subtract --- diff --git a/lib/underscore.date.js b/lib/underscore.date.js index 8d1670845..cd64fbadd 100644 --- a/lib/underscore.date.js +++ b/lib/underscore.date.js @@ -208,17 +208,17 @@ // 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) { diff --git a/test/date.js b/test/date.js index 284c9ec84..4b46ec960 100644 --- a/test/date.js +++ b/test/date.js @@ -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