Dates: Date Comparisons

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


Comparing two dates

You can calculate the difference between two dates using Date.getTime():

const date1 = new Date('July 10, 2018 07:22:13')
const date2 = new Date('July 22, 2018 07:22:13')
const diff = date2.getTime() - date1.getTime() //difference in milliseconds

In the same way you can check if two dates are equal:

const date1 = new Date('July 10, 2018 07:22:13')
const date2 = new Date('July 10, 2018 07:22:13')
if (date2.getTime() === date1.getTime()) {
  //dates are equal
}

Keep in mind that getTime() returns the number of milliseconds, so you need to factor in time in the comparison. July 10, 2018 07:22:13 is not equal to July 10, 2018. In this case you can use setHours(0, 0, 0, 0) to reset the time.

Checking if a date is today

How can you determine if a JavaScript Date object instance is a representation of a date/time that is “today”?

Given a Date instance, we can use the getDate(), getMonth() and getFullYear() methods, which return the day, month and year of a date, and compare them to today, which can be retrieved using new Date().

Here’s a small function that does exactly that, returning true if the argument is today:

const isToday = (someDate) => {
  const today = new Date()
  return someDate.getDate() == today.getDate() &&
    someDate.getMonth() == today.getMonth() &&
    someDate.getFullYear() == today.getFullYear()
}

You can use it like this:

const today = isToday(myDate)

Checking if two dates are the same day

How do you detect if a date object instance in JavaScript refers to the same day of another date object?

JavaScript does not provide this functionality in its standard library, but you can implement it using the methods:

  • getDate() returns the day
  • getMonth() returns the month
  • getFullYear() returns the 4-digits year

This is a simple function you can copy/paste to do the check:

const datesAreOnSameDay = (first, second) =>
    first.getFullYear() === second.getFullYear() &&
    first.getMonth() === second.getMonth() &&
    first.getDate() === second.getDate();

Example usage:

datesAreOnSameDay(new Date(), new Date()) //true

Checking if a date is in the past

Given a JavaScript date, how do you check if it references a day in the past?

Just comparing them using getTime() was not enough, as dates could have a different time.

Here’s a function that works:

const firstDateIsPastDayComparedToSecond = (firstDate, secondDate) => {
  if (firstDate.setHours(0,0,0,0) - secondDate.setHours(0,0,0,0) >= 0) {
    //first date is in future, or it is today
    return false
  }

  return true
}

We use setHours() to make sure we compare 2 dates at the same time (00:00:00).

Here is the same function with the implicit return, less bloated:

const firstDateIsPastDayComparedToSecond = (firstDate, secondDate) =>
  firstDate.setHours(0,0,0,0) - secondDate.setHours(0,0,0,0) < 0

And here is how to use it with a simple example, comparing yesterday to today:

const today = new Date()
const yesterday = new Date(today)

yesterday.setDate(yesterday.getDate() - 1)

firstDateIsPastDayComparedToSecond(yesterday, today) //true
firstDateIsPastDayComparedToSecond(today, yesterday) //false

Lessons in this unit:

0: Introduction
1: Date Basics
2: Getting Date Parts
3: Date Formatting
4: Date Math
5: ▶︎ Date Comparisons
6: Date Recipes