Skip to main content

How to Check if a Date is Today, Yesterday, or Tomorrow in JavaScript

When working with dates, a common requirement is to determine if a date is "today," "yesterday," or "tomorrow." This is useful for formatting timestamps in a user-friendly way (e.g., displaying "Today at 10:30 AM") or for filtering data. The key to this task is to compare the date portion of two Date objects while completely ignoring the time.

This guide will teach you the standard and most reliable method for this comparison using the .toDateString() method. You will learn how to build simple, reusable functions to check if a date is today, yesterday, or tomorrow.

The Core Challenge: Comparing Dates Without Time

If you compare two Date objects directly with ===, you are checking if they are the exact same object in memory, which is almost never what you want. If you compare them with ==, you are comparing their millisecond timestamps.

const date1 = new Date('2023-10-27T10:00:00');
const date2 = new Date('2023-10-27T11:30:00');

console.log(date1 == date2); // Output: false
note

Even though these two dates are on the same day, they are not equal because their time components are different. To check if a date is "today," we need a way to compare only the year, month, and day.

The Date.prototype.toDateString() method is the perfect tool for this. It returns a human-readable string that represents only the date portion of a Date object, in a consistent format.

const myDate = new Date('2023-10-27T10:30:00');
console.log(myDate.toDateString());
// Output: "Fri Oct 27 2023"
note

By converting two Date objects to this string format, we can compare them. If their date strings are identical, we know they fall on the same day, regardless of their time.

Solution 1: Check if a Date is Today

To check if a date is today, we compare its date string to the date string of the current date.

function isToday(date) {
const today = new Date();

// Compare the year, month, and day of the two dates
return date.toDateString() === today.toDateString();
}

const someDate1 = new Date(); // Today
console.log(`Is this date today? ${isToday(someDate1)}`);
// Output: Is this date today? true

const someDate2 = new '2021-01-01'(); // A past date
console.log(`Is this date today? ${isToday(someDate2)}`);
// Output: Is this date today? false

Solution 2: Check if a Date is Yesterday

The logic is very similar. First, we get today's date and subtract one day from it to get yesterday's date. Then, we compare the date strings.

function isYesterday(date) {
const yesterday = new Date();

// Use .setDate() to subtract one day from today
yesterday.setDate(yesterday.getDate() - 1);

return date.toDateString() === yesterday.toDateString();
}

const someDate1 = new Date();
someDate1.setDate(someDate1.getDate() - 1); // Yesterday
console.log(`Is this date yesterday? ${isYesterday(someDate1)}`);
// Output: Is this date yesterday? true

const someDate2 = new Date(); // Today
console.log(`Is this date yesterday? ${isYesterday(someDate2)}`);
// Output: Is this date yesterday? false
note

How it Works: The date.setDate() method is smart. If you set the day to a number that is less than 1, it will automatically roll back to the previous month and year correctly.

Solution 3: Check if a Date is Tomorrow

Following the same pattern, we can check for tomorrow by adding one day to the current date.

function isTomorrow(date) {
const tomorrow = new Date();

// Use .setDate() to add one day to today
tomorrow.setDate(tomorrow.getDate() + 1);

return date.toDateString() === tomorrow.toDateString();
}

const someDate1 = new Date();
someDate1.setDate(someDate1.getDate() + 1); // Tomorrow
console.log(`Is this date tomorrow? ${isTomorrow(someDate1)}`);
// Output: Is this date tomorrow? true

const someDate2 = new Date(); // Today
console.log(`Is this date tomorrow? ${isTomorrow(someDate2)}`);
// Output: Is this date tomorrow? false

Conclusion

Comparing dates without considering the time is a common requirement, and the .toDateString() method provides a simple and reliable solution.

The key takeaways are:

  1. Directly comparing Date objects checks their exact time, which is not what you want when checking for "today" or "yesterday."
  2. The .toDateString() method returns a consistent string format for the date portion only (e.g., "Fri Oct 27 2023").
  3. To check if a date is today, compare its date string with new Date().toDateString().
  4. To check for yesterday or tomorrow, first calculate that date by using setDate() with getDate() - 1 or getDate() + 1, and then compare the date strings.