Skip to content

Commit

Permalink
Add quarter diff support
Browse files Browse the repository at this point in the history
  • Loading branch information
ichernev committed Dec 29, 2014
1 parent 436371c commit d16828e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions test/moment/quarter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d16828e

Please sign in to comment.