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.
Asked
Active
Viewed 674 times
2
-
https://stackoverflow.com/questions/44800471/check-if-times-overlap-using-moment – huan feng Jul 28 '20 at 07:33
1 Answers
1
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.
}

Alberto Chiesa
- 7,022
- 2
- 26
- 53
-
Thank you for your quick response. It looks really helpful. I am trying to implement it right now. – denis.shtupa Jul 28 '20 at 08:19