# Check if a date is in the past in JavaScript

> Learn how to check if a JavaScript date falls on a past day compared to another, using setHours(0,0,0,0) to ignore the time and compare only the calendar day.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2019-10-16 | Updated: 2019-10-24 | Topics: [JavaScript](https://flaviocopes.com/tags/js/) | Canonical: https://flaviocopes.com/how-to-check-date-is-past-javascript/

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:

```js
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

```js
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:

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

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

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