javascript calculation time difference

class ts {
    constructor() { this._ms = ['Month', 'Date', 'Hours', 'Minutes', 'Seconds', 'Milliseconds']; }
    _f(a, b, r, i) {
        var m = this._ms;
        for (; i < m.length; i++) {
            var t = 'get' + m[i];
            t = a[t]() - b[t]();
            if (t > 0) return r;
            if (t < 0) return r - 1;
        }
        return r;
    }
    year(a, b) {
        if (a.getTime() < b.getTime()) {
            var t = a;
            a = b;
            b = t;
        }
        return this._f(a, b, a.getFullYear() - b.getFullYear(), 0);
    }
    month(a, b) {
        if (a.getTime() < b.getTime()) {
            var t = a;
            a = b;
            b = t;
        }
        return this._f(a, b, 12 * (a.getFullYear() - b.getFullYear()) + (a.getMonth() - b.getMonth()), 1);
    }
    date(a, b) {
        return parseInt(Math.abs(a.getTime() - b.getTime()) / (1000 * 24 * 60 * 60));
    }
}

 

Guess you like

Origin blog.csdn.net/slwsss/article/details/103195067