Jquery插件:计算两个时间区间的交叉部分

Jquery插件:计算两个时间区间的交叉部分

(function($) {
    var diff = function(sDate, eDate) {
        var s = new Date(sDate);
        var e = new Date(eDate);
        var d = s.getTime() - e.getTime();
        return d;
    };

    var max = function(start, end) {
        if (diff(start, end) < 0) {
            return end;
        } else {
            return start;
        }
    };

    var min = function(start, end) {
        if (diff(start, end) < 0) {
            return start;
        } else {
            return end;
        }
    };

    var getTimeInterval = function(a, b, c, d) {
        var start = max(a, c);
        var end = min(b, d);
        return diff(end, start);
    };

    $.extend({
        getTimeInterval: getTimeInterval,
        getDiff: diff,
        getMaxDate:max,
        getMinDate:min
    });
})(jQuery);

点赞