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
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 {
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);