Use timestamp instead of Date object in order to efficiently compare ranges. This as well will be much more efficient for Database to index by timestamp.
BearIf you don't know which of time is earlier in mind that xpair of dates for timespans, then you can be after ydo min
and vice versamax
checks. That why I've checked by distanceBut if you do know which time is before and which after, no min
, max
in this examplecondition required.
Condition bellow checks if timespans Overlap which means it will be true
even if only last second of first timespan overlaps with first second from second timespan.
You can do other checks for Contain and identify which of them contain which, but thats different condition.
// time of first timespan
var x = new Date('01/01/2001 8:30:00').getTime(); // time x
var y = new Date('01/01/2001 9:30:00').getTime();
// time yof second timespan
var za = new Date('01/01/2001 8:54:00').getTime(); // time to check
var db = Math.abs(x - y);new Date('01/01/ time2001 distance9:00:00').getTime();
if (z - Math.min(x, y) <= d) {
Math.max(a, b) //&& inMath.max(x, betweeny) of>= xMath.min(a, andb)) y{
} else {
// outside of x and y time rangebetween
}