From: Iskren Chernev Date: Mon, 29 Dec 2014 09:10:10 +0000 (+0200) Subject: Add quarter diff support X-Git-Tag: 2.9.0~9^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2131%2Fhead;p=thirdparty%2Fmoment.git Add quarter diff support --- diff --git a/moment.js b/moment.js index 9057be4d8..19a416929 100644 --- a/moment.js +++ b/moment.js @@ -2270,7 +2270,7 @@ units = normalizeUnits(units); - if (units === 'year' || units === 'month') { + if (units === 'year' || units === 'month' || units === 'quarter') { // average number of days in the months in the given dates diff = (this.daysInMonth() + that.daysInMonth()) * 432e5; // 24 * 60 * 60 * 1000 / 2 // difference in months @@ -2283,7 +2283,9 @@ daysAdjust += ((this.utcOffset() - moment(this).startOf('month').utcOffset()) - (that.utcOffset() - moment(that).startOf('month').utcOffset())) * 6e4; output += daysAdjust / diff; - if (units === 'year') { + if (units === 'quarter') { + output = output / 3; + } else if (units === 'year') { output = output / 12; } } else { diff --git a/test/moment/quarter.js b/test/moment/quarter.js index f8a2f4125..cea5f8473 100644 --- a/test/moment/quarter.js +++ b/test/moment/quarter.js @@ -119,6 +119,21 @@ exports.quarter = { test.done(); }, + 'quarter diff' : function (test) { + test.equal(moment('2014-01-01').diff(moment('2014-04-01'), 'quarter'), + -1, 'diff -1 quarter'); + test.equal(moment('2014-04-01').diff(moment('2014-01-01'), 'quarter'), + 1, 'diff 1 quarter'); + test.equal(moment('2014-05-01').diff(moment('2014-01-01'), 'quarter'), + 1, 'diff 1 quarter'); + test.ok(Math.abs((4 / 3) - moment('2014-05-01').diff( + moment('2014-01-01'), 'quarter', true)) < 0.00001, + 'diff 1 1/3 quarter'); + test.equal(moment('2015-01-01').diff(moment('2014-01-01'), 'quarter'), + 4, 'diff 4 quarters'); + test.done(); + }, + 'quarter setter bubble to previous year' : function (test) { var m; test.expect(7);