场景一

Time range 1 : 2016-12-06 11:00 to 2016-12-06 12:00
Time range 2 : 2016-12-06 10:00 to 2016-12-06 13:00
time range 1 is completely conflict with time range 2 and vice versa

场景二
Time range 1 : 2016-12-06 11:00 to 2016-12-06 12:00
Time range 2 : 2016-12-06 11:00 to 2016-12-06 14:00
time range 1 is partial conflict with time range 2 and vice versa

场景3
Time range 1 : 2016-12-06 11:00 to 2016-12-06 12:00
Time range 2 : 2016-12-06 09:00 to 2016-12-06 12:00
time range 1 is partial conflict with time range 2 and vice versa

如何使用momentjs完成上述场景?我尝试了 isBetween 函数,但无法正常工作。

最佳答案

我认为 moment-range 就是你要找的。使用它的 overlapscontains 函数来制作它。您可以从 cdnjs servers 链接到 moment-range 。

var date1 = [moment("2016-12-06 11:00"), moment("2016-12-06 12:00")];
var date2 = [moment("2016-12-06 10:00"), moment("2016-12-06 13:00")];

var range  = moment.range(date1);
var range2 = moment.range(date2);

// has overlapping
if(range.overlaps(range2)) {
    if((range2.contains(range, true) || range.contains(range2, true)) && !date1[0].isSame(date2[0]))
    alert("time range 1 is completely conflict with time range 2 and vice versa");
  else
    alert("time range 1 is partially conflict with time range 2 and vice versa");
}

关于javascript - 使用 momentjs 检查此时间范围是否与其他时间范围冲突,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40970369/

10-09 15:38