简体   繁体   English

检查两个以上的日期范围是否重叠

[英]Check if more than two date ranges overlap

I have multiple date ranges.我有多个日期范围。 I want to check if they are overlapping in javascript.我想检查它们是否在 javascript 中重叠。 When there are only two it is easy, I use:当只有两个时很容易,我使用:

if(start_times1 <= end_times2 && end_times1 >= start_times2) {}

But what is the formula when there are more than 2 date ranges?但是当有超过 2 个日期范围时,公式是什么?

You can use nested for loops with arguments您可以使用带arguments嵌套for循环

function dateRangeOverlaps(a_start, a_end, b_start, b_end) {
    if (a_start <= b_start && b_start <= a_end) return true; // b starts in a
    if (a_start <= b_end   && b_end   <= a_end) return true; // b ends in a
    if (b_start <  a_start && a_end   <  b_end) return true; // a in b
    return false;
}
function multipleDateRangeOverlaps() {
    var i, j;
    if (arguments.length % 2 !== 0)
        throw new TypeError('Arguments length must be a multiple of 2');
    for (i = 0; i < arguments.length - 2; i += 2) {
        for (j = i + 2; j < arguments.length; j += 2) {
            if (
                dateRangeOverlaps(
                    arguments[i], arguments[i+1],
                    arguments[j], arguments[j+1]
                )
            ) return true;
        }
    }
    return false;
}

Here is refined version of what Paul posted:这是保罗发布的内容的精炼版本:

  • Added filter and null check to allow any number of entries添加过滤器和空检查以允许任意数量的条目
  • Changed the logic so that it can be applied on an array.更改了逻辑,使其可以应用于数组。 Eg: [{"from": value, "to": value}]例如:[{"from": value, "to": value}]
  • Adjusted overlap check to allow times having end and start as same调整重叠检查以允许结束和开始的时间相同

Script:脚本:

function dateRangeOverlaps(a_start, a_end, b_start, b_end) {
    if (a_start < b_start && b_start < a_end) return true; // b starts in a
    if (a_start < b_end   && b_end   < a_end) return true; // b ends in a
    if (b_start <  a_start && a_end   <  b_end) return true; // a in b
    return false;
}

function multipleDateRangeOverlaps(timeEntries) {
    let i = 0, j = 0;
    let timeIntervals = timeEntries.filter(entry => entry.from != null && entry.to != null && entry.from.length === 8 && entry.to.length === 8);

    if (timeIntervals != null && timeIntervals.length > 1)
    for (i = 0; i < timeIntervals.length - 1; i += 1) {
        for (j = i + 1; j < timeIntervals.length; j += 1) {
                if (
                dateRangeOverlaps(
            timeIntervals[i].from.getTime(), timeIntervals[i].to.getTime(),
            timeIntervals[j].from.getTime(), timeIntervals[j].to.getTime()
                    )
                ) return true;
            }
        }
   return false;
}

Below code comes from my project, maybe it will help you:下面的代码来自我的项目,也许它会帮助你:

 function dateRangeOverlaps(startDateA, endDateA, startDateB, endDateB) { if ((endDateA < startDateB) || (startDateA > endDateB)) { return null } var obj = {}; obj.startDate = startDateA <= startDateB ? startDateB : startDateA; obj.endDate = endDateA <= endDateB ? endDateA : endDateB; return obj; }

Wouldn't be too hard to do recursively.递归执行不会太难。 Make a method overlap which returns the overlapping daterange for two dates.制作一个方法overlap ,它返回两个日期的重叠日期范围。 Then in your hasOverlap(list dates) method, if the list is two items, it's simple, else, return hasoverlap(overlap(dates[0], dates[1]), rest of list)然后在您的hasOverlap(list dates)方法中,如果列表是两个项目,则很简单,否则,返回hasoverlap(overlap(dates[0], dates[1]), rest of list)

//storing existing dates for comparison //存储现有日期进行比较

public multipleExistingDates=[
         {startDate:'02/03/2020 05:00:00',endDate:'02/03/2020 05:30:00'},
         {startDate:02/04/2020 05:00:00'',endDate:'02/05/2020 05:00:00'},]

/ The date to be compared with existing dates to check if the new date is overlapping with existing dates / /要与现有日期进行比较以检查新日期是否与现有日期重叠的日期/

public checkOverlappingDsates(startDate:Date, endDate:Date):boolean{
  return this.multipleExistingDates.some((elem)=>{
       return( !((moment(endDate).diff(moment(elem.startDate))) < 0 || 
              (moment(startDate).diff(moment(elem.endDate))) > 0;})

Note: If the date is overlapping, the function return true else false.注意:如果日期重叠,则函数返回 true 否则返回 false。 Also , you would need to install moment for date comparison.此外,您需要安装 moment 以进行日期比较。

Why don't we use moment and moment-range, is it not supported across all browsers?为什么我们不使用 moment 和 moment-range,不是所有浏览器都支持吗? 🤔 🤔

 window['moment-range'].extendMoment(moment); const events1 = [{ "Date": "05/15/2021", "EndTime": "17:00", "StartTime": "16:00" }, { "Date": "05/15/2021", "EndTime": "18:00", "StartTime": "17:00" }, { "Date": "05/15/2021", "EndTime": "18:45", "StartTime": "17:45" } ]; const events2 = [{ "Date": "05/15/2021", "EndTime": "17:00", "StartTime": "16:00" }, { "Date": "05/15/2021", "EndTime": "18:00", "StartTime": "17:00" }, { "Date": "05/15/2021", "EndTime": "19:45", "StartTime": "18:45" } ]; function checkOverlap(timeSegments) { var overlap = timeSegments .map(r => timeSegments.filter(q => q != r).map(q => moment.range( moment(q.Date + " " + q.StartTime), moment(q.Date + " " + q.EndTime) ).overlaps( moment.range( moment(r.Date + " " + r.StartTime), moment(r.Date + " " + r.EndTime) ) ) ) ); console.log(overlap.map(x => x.includes(true)).includes(true)); } checkOverlap(events1); checkOverlap(events2);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-range/4.0.2/moment-range.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM