Emmanuel Akinfulubi

JavaScriptDate

Check if two date ranges overlap in JavaScript

Intermediate · 4 min read

A hotel can't sell the same room twice for the same nights. A meeting room can't hold two meetings at 10:00. A holiday planner should warn you when two people on one team are away at once. All of these ask the same question: do two date ranges overlap?

In this tutorial you will write overlaps(aStart, aEnd, bStart, bEnd), which answers true or false in a single line. You will also write a second version for ranges that are allowed to touch, like back-to-back meetings. You need to know how to make a date with new Date(year, month, day) and that months count from 0, so January is 0.

Comparing dates with < and <=

<, <=, > and >= work directly on dates. JavaScript turns each date into its timestamp, the number of milliseconds since 1970, and compares the numbers. An earlier date has a smaller number:

JavaScript

console.log(new Date(2026, 0, 1) < new Date(2026, 0, 2)); // true

A range here is just two dates: a start and an end. Range A runs from aStart to aEnd, and range B from bStart to bEnd.

Think about when ranges miss

Draw two ranges as bars on a line. There are many ways for them to overlap: B can start inside A, end inside A, sit completely inside A, or cover all of A. Listing those cases is slow and easy to get wrong.

There are only two ways for them to miss each other:

  1. A ends before B starts.
  2. B ends before A starts.

If neither of those is true, they must overlap. That idea is the whole exercise.

Here is the exercise. Try it on your own first if you like, then read on and we will build it step by step.

41 Do two date ranges overlap?

fair

Given two ranges, each with a start and an end, decide whether they overlap. Two comparisons are enough.

Stuck? Three hints, smallest first
  1. 1. Do not enumerate the cases. There are four ways to overlap and only two ways not to.
  2. 2. They miss each other only if one ends before the other starts.
  3. 3. So overlap is aStart <= bEnd && bStart <= aEnd.

Step 1: write the "they miss" check

A small helper keeps the examples short. d(10) means 10 January 2026.

Step 1

const d = (day) => new Date(2026, 0, day);
const miss = (aStart, aEnd, bStart, bEnd) => aEnd < bStart || bEnd < aStart;
console.log(miss(d(1), d(15), d(20), d(25))); // true
console.log(miss(d(1), d(15), d(10), d(20))); // false

|| means "or". The first pair, 1 to 15 and 20 to 25, miss each other because A ends on the 15th, before B starts on the 20th. The second pair share the days from the 10th to the 15th, so they don't miss.

Step 2: the wrong turn, listing the cases

Most people start by listing the ways to overlap. Here is a version that checks "A starts inside B" or "A ends inside B":

Step 2

const overlapsByCases = (aStart, aEnd, bStart, bEnd) =>
  (aStart >= bStart && aStart <= bEnd) ||
  (aEnd >= bStart && aEnd <= bEnd);
console.log(overlapsByCases(d(1), d(15), d(10), d(20))); // true
console.log(overlapsByCases(d(1), d(15), d(5), d(8)));   // false, but 5 to 8 is inside 1 to 15

It looks reasonable, and it is wrong. When B sits completely inside A, A neither starts nor ends inside B. You would need two more cases to fix it. There is a shorter way.

Step 3: flip the "miss" check

Overlap is simply "not miss". To flip a check like this, two rules help:

  • "not (X or Y)" is the same as "not X and not Y".
  • "not (a < b)" is the same as "a >= b".

So "not (aEnd < bStart or bEnd < aStart)" becomes "aEnd >= bStart and bEnd >= aStart". Turned around so it reads left to right, that is:

Step 3

const overlaps = (aStart, aEnd, bStart, bEnd) => aStart <= bEnd && bStart <= aEnd;
console.log(overlaps(d(1), d(15), d(10), d(20))); // true
console.log(overlaps(d(1), d(15), d(20), d(25))); // false
console.log(overlaps(d(1), d(15), d(5), d(8)));   // true
console.log(overlaps(d(5), d(8), d(1), d(15)));   // true

Two comparisons, and every case is covered, including the one that broke Step 2. The order you pass the ranges in doesn't matter either.

Step 4: ranges that touch

What about 1 to 15 and 15 to 20? They share exactly one point, the 15th.

Step 4

const overlapsHalfOpen = (aStart, aEnd, bStart, bEnd) => aStart < bEnd && bStart < aEnd;
console.log(overlaps(d(1), d(15), d(15), d(20)));         // true
console.log(overlapsHalfOpen(d(1), d(15), d(15), d(20))); // false

overlaps uses <=, so the end of a range counts as part of it. Ranges that touch overlap. That is right for days: a promotion that runs 1 to 15 January and another that runs 15 to 20 January are both live on the 15th.

overlapsHalfOpen uses <, so the end does not count. This is called a half-open range: the start is included, the end is not. Touching ranges don't overlap.

Step 5: back-to-back meetings

Half-open is the right choice for times. A meeting from 9:00 to 10:00 and one from 10:00 to 11:00 don't clash.

Step 5

const at = (hour) => new Date(2026, 0, 15, hour);
console.log(overlapsHalfOpen(at(9), at(10), at(10), at(11))); // false
console.log(overlapsHalfOpen(at(9), at(11), at(10), at(12))); // true

The first pair are back to back, so a booking system should allow them. The second pair really do clash between 10:00 and 11:00.

The full solution

The panel has both versions. It prints a pair that overlaps, a pair that doesn't, and the touching pair read both ways. The checks also cover a range inside another, a range compared with itself and single-day ranges. Press Run to see the results.

The answer

// Inclusive ranges: two comparisons, derived by negating "they miss".
const overlaps = (aStart, aEnd, bStart, bEnd) => aStart <= bEnd && bStart <= aEnd;

// Half-open ranges, where touching does not count as overlapping.
const overlapsHalfOpen = (aStart, aEnd, bStart, bEnd) => aStart < bEnd && bStart < aEnd;

const d = (day) => new Date(2026, 0, day);      // January 2026, for brevity

// A 1-15, B 10-20, C 20-25, inside 5-8, touching 15-20.
console.log('1-15 vs 10-20:', overlaps(d(1), d(15), d(10), d(20)));
console.log('1-15 vs 20-25:', overlaps(d(1), d(15), d(20), d(25)));
console.log('1-15 vs 15-20:', overlaps(d(1), d(15), d(15), d(20)), '<- touching, inclusive');
console.log('1-15 vs 15-20:', overlapsHalfOpen(d(1), d(15), d(15), d(20)), '<- touching, half-open');

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('ranges that share days overlap', overlaps(d(1), d(15), d(10), d(20)), true);
test('ranges that do not, do not', overlaps(d(1), d(15), d(20), d(25)), false);
test('one range wholly inside another overlaps', overlaps(d(1), d(15), d(5), d(8)), true);
test('and it does not matter which is passed first', overlaps(d(5), d(8), d(1), d(15)), true);
test('a range overlaps itself', overlaps(d(1), d(15), d(1), d(15)), true);
test('ranges that touch at one point overlap, inclusively', overlaps(d(1), d(15), d(15), d(20)), true);
test('but not on the half-open reading', overlapsHalfOpen(d(1), d(15), d(15), d(20)), false);
test('a single-day range inside a longer one overlaps', overlaps(d(10), d(10), d(1), d(15)), true);
test('and outside it does not', overlaps(d(30), d(30), d(1), d(15)), false);

The temptation is to enumerate: A starts inside B, A ends inside B, A contains B, B contains A. That is four conditions, easy to get wrong, and unnecessary — two ranges fail to overlap only when one finishes before the other begins, so negating that gives aStart <= bEnd && bStart <= aEnd and nothing else is needed.

Inclusive or half-open is a real decision, not a detail. For meeting rooms, a booking that ends at 10:00 and one that starts at 10:00 do not clash, so half-open is right. For "which days did this promotion run", inclusive is right. The touching assertions above are the two readings side by side.

Common mistakes

  • Listing the ways to overlap. It is easy to miss the "one range inside the other" case, as Step 2 did. Negate "they miss" instead.
  • Mixing < and <= without deciding. Pick inclusive or half-open on purpose for each kind of range, then use it everywhere.
  • Passing a range with its start after its end. The formula assumes each start comes on or before its end. If users can type the dates, swap a backwards pair first.

Try it yourself

  1. Write clashesWithAny(start, end, bookings), where bookings is an array of { start, end } objects. Use some and overlapsHalfOpen.
  2. Return the shared part of two overlapping ranges: it starts at the later start and ends at the earlier end.
  3. Write overlapDays(aStart, aEnd, bStart, bEnd) that counts how many days two inclusive day ranges share, and 0 when they don't overlap.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.