From d16828e7469eabb43b500298b766e234fe8a459c Mon Sep 17 00:00:00 2001 From: Iskren Chernev Date: Mon, 29 Dec 2014 11:10:10 +0200 Subject: [PATCH] Add quarter diff support --- moment.js | 6 ++++-- test/moment/quarter.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/moment.js b/moment.js index 9057be4d83..19a4169296 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 f8a2f41259..cea5f84732 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);