How to check if a date refers to a day in the past in JavaScript
Given a JavaScript date, how do you check if it references a day in the past?
I had this problem: I wanted to check if a date referred to a past day, compared to another date.
Just comparing them using getTime()
was not enough, as dates could have a different time.
I ended up using this function:
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
}
I 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
→ I wrote 17 books to help you become a better developer:
- C Handbook
- Command Line Handbook
- CSS Handbook
- Express Handbook
- Git Cheat Sheet
- Go Handbook
- HTML Handbook
- JS Handbook
- Laravel Handbook
- Next.js Handbook
- Node.js Handbook
- PHP Handbook
- Python Handbook
- React Handbook
- SQL Handbook
- Svelte Handbook
- Swift Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025