Sha256: d2461a45ecae36bb7be4b62d5f17900b13a829e85f1ba293e6c473467ba95df1

Contents?: true

Size: 1.75 KB

Versions: 19

Compression:

Stored size: 1.75 KB

Contents

import absFloor from '../utils/abs-floor';
import { cloneWithOffset } from '../units/offset';
import { normalizeUnits } from '../units/aliases';

export function diff (input, units, asFloat) {
    var that = cloneWithOffset(input, this),
        zoneDelta = (that.utcOffset() - this.utcOffset()) * 6e4,
        delta, output;

    units = normalizeUnits(units);

    if (units === 'year' || units === 'month' || units === 'quarter') {
        output = monthDiff(this, that);
        if (units === 'quarter') {
            output = output / 3;
        } else if (units === 'year') {
            output = output / 12;
        }
    } else {
        delta = this - that;
        output = units === 'second' ? delta / 1e3 : // 1000
            units === 'minute' ? delta / 6e4 : // 1000 * 60
            units === 'hour' ? delta / 36e5 : // 1000 * 60 * 60
            units === 'day' ? (delta - zoneDelta) / 864e5 : // 1000 * 60 * 60 * 24, negate dst
            units === 'week' ? (delta - zoneDelta) / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst
            delta;
    }
    return asFloat ? output : absFloor(output);
}

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'),
        anchor2, adjust;

    if (b - anchor < 0) {
        anchor2 = a.clone().add(wholeMonthDiff - 1, 'months');
        // linear across the month
        adjust = (b - anchor) / (anchor - anchor2);
    } else {
        anchor2 = a.clone().add(wholeMonthDiff + 1, 'months');
        // linear across the month
        adjust = (b - anchor) / (anchor2 - anchor);
    }

    return -(wholeMonthDiff + adjust);
}

Version data entries

19 entries across 19 versions & 4 rubygems

Version Path
active_record_survey_api-0.0.19 spec/test_app/vendor/assets/bower_components/moment/src/lib/moment/diff.js
active_record_survey_api-0.0.18 spec/test_app/vendor/assets/bower_components/moment/src/lib/moment/diff.js
active_record_survey_api-0.0.17 spec/test_app/vendor/assets/bower_components/moment/src/lib/moment/diff.js
active_record_survey_api-0.0.14 spec/test_app/vendor/assets/bower_components/moment/src/lib/moment/diff.js
active_record_survey_api-0.0.12 spec/test_app/vendor/assets/bower_components/moment/src/lib/moment/diff.js
active_record_survey_api-0.0.11 spec/test_app/vendor/assets/bower_components/moment/src/lib/moment/diff.js
active_record_survey_api-0.0.7 spec/test_app/vendor/assets/bower_components/moment/src/lib/moment/diff.js
active_record_survey_api-0.0.6 spec/test_app/vendor/assets/bower_components/moment/src/lib/moment/diff.js
date_picker-0.0.8 test/dummy/vendor/assets/components/moment/src/lib/moment/diff.js
date_picker-0.0.7 test/dummy/vendor/assets/components/moment/src/lib/moment/diff.js
sedge_cms-0.0.1 vendor/assets/components/moment/src/lib/moment/diff.js
date_picker-0.0.6 test/dummy/vendor/assets/components/moment/src/lib/moment/diff.js
date_picker-0.0.5 test/dummy/vendor/assets/components/moment/src/lib/moment/diff.js
date_picker-0.0.4 test/dummy/vendor/assets/components/moment/src/lib/moment/diff.js
date_picker-0.0.3 test/dummy/vendor/assets/components/moment/src/lib/moment/diff.js
date_picker-0.0.2 test/dummy/vendor/assets/components/moment/src/lib/moment/diff.js
date_picker-0.0.1 test/dummy/vendor/assets/components/moment/src/lib/moment/diff.js
puppet-herald-0.8.1 lib/puppet-herald/public/bower_components/moment/src/lib/moment/diff.js
puppet-herald-0.8.0 lib/puppet-herald/public/bower_components/moment/src/lib/moment/diff.js