• 0
  • 0

JS 判断时间区间是否重叠

2020-07-06 25 0 admin 所属分类:Javascript
var data = ['22:00-02:00','03:00-21:00']
function isCross(data) {
    // 正向匹配
    for (var i = 0; i < data.length - 1; i++) {
        var one = data[i].split('-');
        for (var k = i + 1; k < data.length; k++) {
            var two = data[k].split('-');
            if ((one[0] > two[0] && one[0] < two[1]) || (one[1] > two[0] && one[0] < two[1])) {
                return true;
            }
        };
    };
    data = data.reverse();
    for (var i = 0; i < data.length - 1; i++) {
        var one = data[i].split('-');
        for (var k = i + 1; k < data.length; k++) {
            var two = data[k].split('-');
            if ((one[0] > two[0] && one[0] < two[1]) || (one[1] > two[0] && one[0] < two[1])) {
                return true;
            }
        };
    };
    return false;
}
console.log(isCross(data))


返回顶部