How to determine if two time intervals overlap

Suppose you have two time intervals, a and b. The following expression will tell you whether or not they overlap:

a.start < b.end && b.start < a.end

Here’s an interactive demo that illustrates how this works. The box labeled a is draggable. Each part of the expression will appear green if it’s true. If both parts of the expression are green, then the two time intervals overlap.

a.start < b.end
&&
b.start < a.end

Allen’s interval algebra

For a more exhaustive treatment of this topic, Allen’s interval algebra provides a formalization of relations between time intervals.

Source code

The source code for the demo is available here: singerworks-time-intervals-0.8.js

Tags

programming