Brought in another asset for Ray's eye form: moment
[openemr.git] / public / assets / moment-2-13-0 / src / lib / moment / diff.js
blob9f4390818980b1d4fb00d67905fc02fe1f6df809
1 import absFloor from '../utils/abs-floor';
2 import { cloneWithOffset } from '../units/offset';
3 import { normalizeUnits } from '../units/aliases';
5 export function diff (input, units, asFloat) {
6     var that,
7         zoneDelta,
8         delta, output;
10     if (!this.isValid()) {
11         return NaN;
12     }
14     that = cloneWithOffset(input, this);
16     if (!that.isValid()) {
17         return NaN;
18     }
20     zoneDelta = (that.utcOffset() - this.utcOffset()) * 6e4;
22     units = normalizeUnits(units);
24     if (units === 'year' || units === 'month' || units === 'quarter') {
25         output = monthDiff(this, that);
26         if (units === 'quarter') {
27             output = output / 3;
28         } else if (units === 'year') {
29             output = output / 12;
30         }
31     } else {
32         delta = this - that;
33         output = units === 'second' ? delta / 1e3 : // 1000
34             units === 'minute' ? delta / 6e4 : // 1000 * 60
35             units === 'hour' ? delta / 36e5 : // 1000 * 60 * 60
36             units === 'day' ? (delta - zoneDelta) / 864e5 : // 1000 * 60 * 60 * 24, negate dst
37             units === 'week' ? (delta - zoneDelta) / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst
38             delta;
39     }
40     return asFloat ? output : absFloor(output);
43 function monthDiff (a, b) {
44     // difference in months
45     var wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month()),
46         // b is in (anchor - 1 month, anchor + 1 month)
47         anchor = a.clone().add(wholeMonthDiff, 'months'),
48         anchor2, adjust;
50     if (b - anchor < 0) {
51         anchor2 = a.clone().add(wholeMonthDiff - 1, 'months');
52         // linear across the month
53         adjust = (b - anchor) / (anchor - anchor2);
54     } else {
55         anchor2 = a.clone().add(wholeMonthDiff + 1, 'months');
56         // linear across the month
57         adjust = (b - anchor) / (anchor2 - anchor);
58     }
60     //check for negative zero, return zero if negative zero
61     return -(wholeMonthDiff + adjust) || 0;