Skip to main content
Actually answer did not used two timespans before, now it does as question asks.
Source Link
moka
  • 23k
  • 4
  • 52
  • 67

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
}

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.
Bear in mind that x can be after y and vice versa. That why I've checked by distance in this example.

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 y
var z = new Date('01/01/2001 8:54:00').getTime(); // time to check
var d = Math.abs(x - y); // time distance

if (z - Math.min(x, y) <= d) {
  // in between of x and y
} else {
  // outside of x and y time range
}

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.
If you don't know which of time is earlier in pair of dates for timespans, then you can do min and max checks. But if you do know which time is before and which after, no min, max in condition 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();
var y = new Date('01/01/2001 9:30:00').getTime(); 

// time of second timespan
var a = new Date('01/01/2001 8:54:00').getTime();
var b = new Date('01/01/2001 9:00:00').getTime();

if (Math.min(x, y) <= Math.max(a, b) && Math.max(x, y) >= Math.min(a, b)) {
    // between
}
Source Link
moka
  • 23k
  • 4
  • 52
  • 67

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.
Bear in mind that x can be after y and vice versa. That why I've checked by distance in this example.

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 y
var z = new Date('01/01/2001 8:54:00').getTime(); // time to check
var d = Math.abs(x - y); // time distance

if (z - Math.min(x, y) <= d) {
  // in between of x and y
} else {
  // outside of x and y time range
}