Skip to content

Commit

Permalink
Merge ad453e2 into 2e2a5b3
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Elsenousi committed Jul 25, 2018
2 parents 2e2a5b3 + ad453e2 commit 6aeec86
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/lib/moment/diff.js
@@ -1,6 +1,10 @@
import absFloor from '../utils/abs-floor';
import { cloneWithOffset } from '../units/offset';
import { normalizeUnits } from '../units/aliases';
import { createDuration } from '../duration/create';
import absRound from '../utils/abs-round';
import { get } from './get-set';
import { daysInMonth } from '../units/month';

export function diff (input, units, asFloat) {
var that,
Expand Down Expand Up @@ -36,19 +40,45 @@ export function diff (input, units, asFloat) {
return asFloat ? output : absFloor(output);
}

function isEndOfMonth(mom) {
return mom.date() === daysInMonth(mom.year(), mom.month());
}

function addWithEndOfMonth(a, b, val) {
if (!a.isValid()) {
return;
}
var dur = createDuration(val, 'months');
var months = absRound(dur._months);
var dayInMonth = a.date();
var newMonth = get(a, 'Month') + months;
var maxDays = daysInMonth(a.year(), newMonth);
// if End of Month, should ignore time difference
if (dayInMonth > maxDays || isEndOfMonth(a)) {
dayInMonth = maxDays;
var h = b._d['get' + (a._isUTC ? 'UTC' : '') + 'Hours']();
var m = b._d['get' + (a._isUTC ? 'UTC' : '') + 'Minutes']();
var s = b._d['get' + (a._isUTC ? 'UTC' : '') + 'Seconds']();
var ms = b._d['get' + (a._isUTC ? 'UTC' : '') + 'Milliseconds']();
a._d['set' + (a._isUTC ? 'UTC' : '') + 'Hours'](h, m, s, ms);
}
a._d['set' + (a._isUTC ? 'UTC' : '') + 'Month'](newMonth, dayInMonth);
return a;
}

function monthDiff (a, b) {
// difference in months
var wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month()),
// b is in (anchor - 1 month, anchor + 1 month)
anchor = a.clone().add(wholeMonthDiff, 'months'),
anchor = addWithEndOfMonth(a.clone(), b, wholeMonthDiff),
anchor2, adjust;

if (b - anchor < 0) {
anchor2 = a.clone().add(wholeMonthDiff - 1, 'months');
anchor2 = addWithEndOfMonth(a.clone(), b, wholeMonthDiff - 1);
// linear across the month
adjust = (b - anchor) / (anchor - anchor2);
} else {
anchor2 = a.clone().add(wholeMonthDiff + 1, 'months');
anchor2 = addWithEndOfMonth(a.clone(), b, wholeMonthDiff + 1);
// linear across the month
adjust = (b - anchor) / (anchor2 - anchor);
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/moment/diff.js
Expand Up @@ -102,6 +102,20 @@ test('diff month', function (assert) {
assert.equal(moment([2011, 0, 31]).diff([2011, 2, 1], 'months'), -1, 'month diff');
});

test('diff month', function (assert) {
assert.equal(moment([2011, 0, 31]).diff([2011, 2, 1], 'months'), -1, 'month diff');
});

test('end of month diff', function (assert) {
assert.equal(moment('2016-02-29').diff('2016-01-30', 'months'), 1, 'Feb 29 to Jan 30 should be 1 month');
assert.equal(moment('2016-02-29').diff('2016-01-31', 'months'), 1, 'Feb 30 to Jan 31 should be 1 month');
assert.equal(moment('2016-05-31').add(1,'month').diff(moment('2016-05-31'), 'month'), 1, '(May 31 plus 1 month) to May 31 should be 1 month diff');
});

test('end of month diff with time', function (assert) {
assert.equal(moment([2017, 2, 31, 1]).diff([2017, 1, 28, 2], 'months'), 1, 'Feb 28 to March 31 should be 1 month');
});

test('diff across DST', function (assert) {
var dst = dstForYear(2012), a, b, daysInMonth;
if (!dst) {
Expand Down

0 comments on commit 6aeec86

Please sign in to comment.