Emmanuel Akinfulubi

JavaScriptDate

Check if a date is between two dates in JavaScript

Intermediate · 4 min read

"Is this booking inside the sale period?", "show orders from 1 to 15 January", "is the coupon still valid today?": checking whether a date falls inside a range is everywhere. The check itself is one line. The bug most people ship is in how the range ends: a report for "1 to 15 January" that quietly leaves out almost all of the 15th.

In this tutorial you will write isWithin(date, from, to), which includes both ends, and isWithinHalfOpen(date, from, to), which includes the start but not the end. Then you will see the last-day bug and two clean ways to fix it. You need to know how to make a date with new Date(year, month, day, hour, minute) and that months count from 0.

Comparing dates with < and >

<, >, <= and >= work directly on dates. They turn each date into its timestamp (milliseconds since 1970) and compare the numbers. An earlier date has a smaller number:

JavaScript

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

So "is date between from and to?" is two comparisons joined with &&, which means "and".

Inclusive and half-open

There are two common ways to describe a range:

  • Inclusive: both ends count. Written date >= from && date <= to.
  • Half-open: the start counts and the end doesn't. Written date >= from && date < to. Mathematicians write this [from, to).

Half-open sounds odd at first, but it is very handy. Ranges like "January" and "February" can then share a boundary, 1 February at midnight, without a gap or an overlap.

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

40 Does a date fall inside a range?

fair

Check whether a date lies between two others. Do it inclusively and half-openly, and show why a range ending at midnight excludes almost all of its last day.

Stuck? Three hints, smallest first
  1. 1. Compare timestamps, or use the relational operators directly — both work on dates.
  2. 2. Inclusive means >= and <=; half-open means >= and <.
  3. 3. A date range written as two calendar dates really means "from the start of the first to the END of the last".

Step 1: the inclusive check

Step 1

const isWithin = (date, from, to) => date >= from && date <= to;
const from = new Date(2026, 0, 1);
const to = new Date(2026, 0, 15);
console.log(isWithin(new Date(2026, 0, 10), from, to));  // true
console.log(isWithin(from, from, to));                   // true
console.log(isWithin(to, from, to));                     // true
console.log(isWithin(new Date(2025, 11, 31), from, to)); // false
console.log(isWithin(new Date(2026, 0, 16), from, to));  // false

A date in the middle is inside, and so are both ends. 31 December and 16 January are outside.

Step 2: the half-open check

Change <= to < for the end:

Step 2

const isWithinHalfOpen = (date, from, to) => date >= from && date < to;
console.log(isWithinHalfOpen(from, from, to)); // true
console.log(isWithinHalfOpen(to, from, to));   // false

The start still counts, but to itself is now outside.

Step 3: the wrong turn, the last day

Here is the surprise. Try 11 pm on 15 January:

Step 3

const lateOnTheLastDay = new Date(2026, 0, 15, 23, 0);
console.log(isWithin(lateOnTheLastDay, from, to)); // false

"1 to 15 January" sounds like fifteen whole days. But new Date(2026, 0, 15) is one exact moment: midnight at the start of the 15th. So the range ends the moment the 15th begins. Anything later that day, at 9 am, noon or 11 pm, is after to and falls outside. A report built this way silently drops almost a whole day of data.

Step 4: fix it by moving the end to the end of the day

One fix keeps the inclusive check and pushes to to the last millisecond of its day, 23:59:59.999:

Step 4

const endOfDay = (date) => {
  const copy = new Date(date);
  copy.setHours(23, 59, 59, 999);
  return copy;
};
console.log(isWithin(lateOnTheLastDay, from, endOfDay(to))); // true

endOfDay works on a copy, so to itself doesn't change.

Step 5: or use half-open with the next day

The other fix uses the half-open check, with the end set to midnight at the start of the day after the range:

Step 5

const dayAfter = new Date(2026, 0, 16);
console.log(isWithinHalfOpen(lateOnTheLastDay, from, dayAfter));        // true
console.log(isWithinHalfOpen(new Date(2026, 0, 16), from, dayAfter));   // false

Everything on the 15th is before midnight on the 16th, so it counts. Midnight on the 16th itself doesn't. Many date libraries and databases work this way, because there is no need to think about the "last millisecond" at all.

Both fixes are fine. What matters is to pick one on purpose and use it everywhere.

Step 6: a backwards range

If from and to are the wrong way round, nothing can be at or after the start and also at or before an earlier end:

Step 6

console.log(isWithin(new Date(2026, 0, 10), to, from)); // false

That is usually what you want. If your users might type the dates backwards, swap them before checking.

The full solution

The panel has both checks and endOfDay. It prints a mid-range date, the 11 pm surprise and the fix. The checks cover both ends, dates outside the range, the half-open end, the last-day bug, the endOfDay fix and a backwards range. Press Run to see them.

The answer

const isWithin = (date, from, to) => date >= from && date <= to;
const isWithinHalfOpen = (date, from, to) => date >= from && date < to;

const endOfDay = (date) => {
  const copy = new Date(date);
  copy.setHours(23, 59, 59, 999);
  return copy;
};

const from = new Date(2026, 0, 1);
const to = new Date(2026, 0, 15);
const lateOnTheLastDay = new Date(2026, 0, 15, 23, 0);

console.log('01/01 to 15/01 contains 10/01 :', isWithin(new Date(2026, 0, 10), from, to));
console.log('...contains 15/01 at 23:00    :', isWithin(lateOnTheLastDay, from, to), '<- surprise');
console.log('...with the end pushed to 23:59:59.999:', isWithin(lateOnTheLastDay, from, endOfDay(to)));

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('a date in the middle is inside', isWithin(new Date(2026, 0, 10), from, to), true);
test('the lower bound is included', isWithin(from, from, to), true);
test('so is the upper bound, at exactly midnight', isWithin(to, from, to), true);
test('but not in the half-open version', isWithinHalfOpen(to, from, to), false);
test('a date before the range is out', isWithin(new Date(2025, 11, 31), from, to), false);
test('and one after it', isWithin(new Date(2026, 0, 16), from, to), false);
test('23:00 on the last day is OUTSIDE, because the range ends at midnight', isWithin(lateOnTheLastDay, from, to), false);
test('extending the end to the last millisecond fixes it', isWithin(lateOnTheLastDay, from, endOfDay(to)), true);
test('a backwards range contains nothing', isWithin(new Date(2026, 0, 10), to, from), false);

The seventh assertion is the bug this exercise exists for. "1 January to 15 January" reads like fifteen whole days, but as timestamps it is 1 January 00:00:00.000 to 15 January 00:00:00.000, so everything after midnight on the 15th falls outside — a report that quietly drops its last day.

Two ways out, and both are fine as long as you choose: push the end to endOfDay, or use a half-open range [from, to) with to set to the start of the day after. Half-open is what most date libraries and most databases use, because adjacent ranges then tile with no gap and no overlap.

Common mistakes

  • Ending a range at midnight on its last day. Almost all of that day falls outside. Use endOfDay(to) or the next day with a half-open check.
  • Mixing inclusive and half-open ranges in one app. Boundary dates end up counted twice or not at all.
  • Comparing with ===. Two separate dates are never ===, so date === to is always false. Use <= and >=, or compare getTime().

Try it yourself

  1. Write isInMonth(date, year, month) using isWithinHalfOpen and the first day of the next month.
  2. Write isWithinDays(date, fromDay, toDay) that takes calendar days and always includes the whole last day.
  3. Filter an array of order times to those between 1 and 15 January 2026, including the 15th.

Keep practising

Newsletter

New writing, when it lands.

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