使用 momentjs 检查此时间范围是否与其他时间范围冲突 [英] use momentjs to check if this time range is conflict with other time range

查看:154
本文介绍了使用 momentjs 检查此时间范围是否与其他时间范围冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景一

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

场景 2

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 功能,但无法正常工作.

How to accomplish above scenario using momentjs? I tried the isBetween function but couldn't get it work.

推荐答案

我认为 moment-range 就是你要找的.使用它的 overlapscontains 函数来实现.您可以从 cdnjs 服务器链接到 moment-range.

I think moment-range is what you are looking for. Use its overlaps and contains function to make it. You can link to moment-range from the cdnjs servers.

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");
}

这篇关于使用 momentjs 检查此时间范围是否与其他时间范围冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆