In this case, i need to validate those three time ranges, and user will only be able to save them if they dont have overlap.
In this case, i need to validate those three time ranges, and user will only be able to save them if they dont have overlap.
Well, the formula for two time ranges is well known: Determine Whether Two Date Ranges Overlap
So, you need to check that this check is verified for any pair, like this:
function areOverlapping(a, b) {
return (a.startA <= b.end) && (a.end >= b.start);
}
function anyOverlap(intervals: any[]) {
for(let i = 0; i < intervals.length - 1; i++) {
for(let j = i + 1; j < intervals.length; j++) {
if (areOverlapping(intervals[i], intervals[j])) return true;
}
}
return false;
}
if (anyOverlap([interval1, interval2, interval3])) {
// there is at least a pair that overlaps.
}