Skip to main content

How to Check if a Date is Between Two Dates in JavaScript

A very common task in JavaScript is to determine if a specific date falls within a given range. You might need to check if a project's due date is within the current month, if a user's subscription is currently active, or if an event occurred within a specific time frame. This is a straightforward comparison, as Date objects in JavaScript can be compared directly.

This guide will teach you the standard method for comparing Date objects to check if a date is between a start and end date. You will also learn how to handle different date string formats and how to check if a date is within a dynamic range, such as "within the last 30 days."

The Core Method: Direct Comparison of Date Objects

The easiest way to check if a date is between two other dates is to use standard comparison operators (>, <, >=, <=). JavaScript automatically converts the Date objects to numerical timestamps for the comparison, so it works just like comparing numbers.

The Logic: date > startDate && date < endDate

  • This expression checks if the date is after the startDate AND before the endDate.

Basic Example: Checking a Date Range

This script checks if a given project date falls within the fourth quarter of 2023.

// The date we want to check
const projectDate = new Date('2023-11-15');

// The start and end of our range
const startDate = new Date('2023-10-01');
const endDate = new Date('2023-12-31');

let isInRange = false;
if (projectDate >= startDate && projectDate <= endDate) {
isInRange = true;
}

console.log(`Is the project date within the fourth quarter? ${isInRange}`);
// Output: Is the project date within the fourth quarter? true
note

This simple and readable comparison is the standard way to handle date ranges.

How Date Comparison Works (Timestamps)

This direct comparison is possible because, under the hood, a JavaScript Date object stores a single number: the number of milliseconds that have elapsed since the Unix Epoch (midnight on January 1, 1970 UTC).

When you use a comparison operator like >, JavaScript calls an internal method (valueOf(), which is equivalent to getTime()) on the Date objects, converting them to these millisecond timestamps before comparing them as numbers.

const myDate = new Date('2023-01-01');
console.log(myDate.getTime()); // Output: 1672531200000

Important: Handling Different Date String Formats

The new Date() constructor is notoriously inconsistent when parsing date strings.

  • new Date('2023-10-27'): This ISO 8601 format (YYYY-MM-DD) is the most reliable and is universally understood.
  • new Date('10/27/2023'): This format (MM/DD/YYYY) is often interpreted correctly in US-based environments but can fail or be misinterpreted in others.

Manual Parsing

note

Best Practice: Whenever possible, use the YYYY-MM-DD format to construct your dates to avoid parsing errors. If you are dealing with other formats, it's best to parse them manually.

Example of manual parsing:

// Handles MM/DD/YYYY format reliably
const dateStr = '10/27/2023';
const [month, day, year] = dateStr.split('/');

// Note: The month is 0-indexed in the Date constructor (0=Jan, 1=Feb, etc.)
const reliableDate = new Date(year, month - 1, day);

console.log(reliableDate);
// Output: Fri Oct 27 2023 ...

Advanced Use Case: Checking if a Date is Within the Last 30 Days

Sometimes, your range is not fixed but is relative to the current date. For this, you need to calculate the start date of your range dynamically.

The logic:

  1. Get the current date and time (new Date()).
  2. Get the timestamp for 30 days ago. You can do this by subtracting 30 days' worth of milliseconds from the current timestamp.
  3. Compare your target date to the current date and the "30 days ago" date.
function isWithinLast30Days(date) {
const today = new Date();
const thirtyDaysAgo = new Date();

// Set the date to 30 days in the past
thirtyDaysAgo.setDate(today.getDate() - 30);

// Check if the date is after thirtyDaysAgo AND before or on today
return date > thirtyDaysAgo && date <= today;
}

const recentDate = new Date();
recentDate.setDate(recentDate.getDate() - 15); // A date from 15 days ago

const oldDate = new Date();
oldDate.setDate(oldDate.getDate() - 45); // A date from 45 days ago

console.log(`Is the recent date within the last 30 days? ${isWithinLast30Days(recentDate)}`);
// Output: Is the recent date within the last 30 days? true

console.log(`Is the old date within the last 30 days? ${isWithinLast30Days(oldDate)}`);
// Output: Is the old date within the last 30 days? false

Conclusion

Checking if a date falls within a range is a straightforward task in JavaScript, thanks to the way Date objects can be compared directly.

The key takeaways are:

  1. You can use standard comparison operators (>, <, >=, <=) to check if a Date object is between a start and end date.
  2. JavaScript performs this comparison using the underlying millisecond timestamp of each date.
  3. For maximum reliability, create your Date objects from strings in ISO 8601 format (YYYY-MM-DD). If you have a different format, parse it manually.
  4. For dynamic ranges (like "last 30 days"), you must first calculate the boundary dates based on the current time.