From: Tim Wood Date: Tue, 25 Oct 2011 16:30:37 +0000 (-0700) Subject: Adding time specific diffs X-Git-Tag: 1.1.0~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b82b34b97f03aedc788176c381120e402a6a220b;p=thirdparty%2Fmoment.git Adding time specific diffs --- diff --git a/build.js b/build.js index 80c5bfbe9..0da5def82 100755 --- a/build.js +++ b/build.js @@ -18,6 +18,7 @@ var JSHINT_CONFIG = { "debug": false, "devel": false, "eqeqeq": true, + "eqnull": true, "evil": false, "forin": false, "immed": false, diff --git a/moment.js b/moment.js index f8244719e..e52a85582 100644 --- a/moment.js +++ b/moment.js @@ -430,8 +430,26 @@ return this; }, - diff : function (input, format) { - return this._d - moment(input, format)._d; + diff : function (input, val, float) { + var inputMoment = moment(input), + diff = this._d - inputMoment._d, + year = this.year() - inputMoment.year(), + month = this.month() - inputMoment.month(), + day = this.day() - inputMoment.day(), + output; + if (val === 'months') { + output = year * 12 + month + day / 30; + } else if (val === 'years') { + output = year + month / 12; + } else { + output = val === 'seconds' ? diff / 1e3 : // 1000 + val === 'minutes' ? diff / 6e4 : // 1000 * 60 + val === 'hours' ? diff / 36e5 : // 1000 * 60 * 60 + val === 'days' ? diff / 864e5 : // 1000 * 60 * 60 * 24 + val === 'weeks' ? diff / 6048e5 : // 1000 * 60 * 60 * 24 * 7 + val === 'days' ? diff / 3600 : diff; + } + return float ? output : round(output); }, from : function (time, withoutSuffix) { diff --git a/test/date.js b/test/date.js index a45b93587..5f33570a8 100755 --- a/test/date.js +++ b/test/date.js @@ -184,6 +184,33 @@ test("diff", 5, function() { equal(moment(oneHourDate).diff(nowDate), 60 * 60 * 1000, "1 hour from now = 360000"); }); +test("diff key after", 7, function() { + equal(moment([2010]).diff([2011], 'years'), -1, "year diff"); + equal(moment([2010]).diff([2011, 6], 'years', true), -1.5, "year diff, float"); + equal(moment([2010]).diff([2010, 2], 'months'), -2, "month diff"); + equal(moment([2010]).diff([2010, 0, 4], 'days'), -3, "day diff"); + equal(moment([2010]).diff([2010, 0, 1, 4], 'hours'), -4, "hour diff"); + equal(moment([2010]).diff([2010, 0, 1, 0, 5], 'minutes'), -5, "minute diff"); + equal(moment([2010]).diff([2010, 0, 1, 0, 0, 6], 'seconds'), -6, "second diff"); +}); + +test("diff key before", 7, function() { + equal(moment([2011]).diff([2010], 'years'), 1, "year diff"); + equal(moment([2011, 6]).diff([2010], 'years', true), 1.5, "year diff, float"); + equal(moment([2010, 2]).diff([2010], 'months'), 2, "month diff"); + equal(moment([2010, 0, 4]).diff([2010], 'days'), 3, "day diff"); + equal(moment([2010, 0, 1, 4]).diff([2010], 'hours'), 4, "hour diff"); + equal(moment([2010, 0, 1, 0, 5]).diff([2010], 'minutes'), 5, "minute diff"); + equal(moment([2010, 0, 1, 0, 0, 6]).diff([2010], 'seconds'), 6, "second diff"); +}); + +test("diff overflow", 4, function() { + equal(moment([2011]).diff([2010], 'months'), 12, "month diff"); + equal(moment([2010, 0, 2]).diff([2010], 'hours'), 24, "hour diff"); + equal(moment([2010, 0, 1, 2]).diff([2010], 'minutes'), 120, "minute diff"); + equal(moment([2010, 0, 1, 0, 4]).diff([2010], 'seconds'), 240, "second diff"); +}); + module("leap year");