# How to check if a date is today in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Aug 7, 2019, updated Oct 8, 2022. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/check-if-date-is-today-javascript

> A protip to check if a date object is equal to today's date in JavaScript.

From a JavaScript date instance, we can get the day, month and year values by using `getDate()`, `getMonth()` and `getFullYear()` methods:

```javascript
// month is zero-based (0-11)
const date = new Date(2022, 7, 7)

date.getDate() // 7
date.getMonth() // 7
date.getFullYear() // 2022
```

Now let's create a small function that takes a date as an argument and compares the above values to today's date values, and returns true if both are the same:

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

Here is how you can use it:

```javascript
const date = new Date(2022, 7, 7)
console.log(isToday(date)) // true
```

Alternatively, you can extend the date object by adding the above function directly to [object prototype](https://attacomsian.com/blog/objects-prototypes-classes-javascript#prototypes) like below:

```javascript
Date.prototype.isToday = function () {
  const today = new Date()
  return this.getDate() === today.getDate() && 
      this.getMonth() === today.getMonth() && 
      this.getFullYear() === today.getFullYear()
}
```

Now just call the `isToday()` method on any date object to compare it with today's date:

```javascript
const date = new Date(2022, 7, 7)
console.log(date.isToday())
```
