Languages
[Edit]
EN

JavaScript - check if date is today

3 points
Created by:
Frida-Timms
607

In this article, we would like to show you how to check if date is today in JavaScript.

In below example we create a reusable isCurrentDate() function that accepts same arguments as Date JavaScript class does and returns boolean that indicates if date part is the same as current date.

Note:

The example works under Node.js too.

Practical example:

// ONLINE-RUNNER:browser;

const isCurrentDate = (...date) => {
    const currentDate = new Date();
    const desiredDate = new Date(...date);
    return currentDate.getDate()     === desiredDate.getDate() &&
           currentDate.getMonth()    === desiredDate.getMonth() &&
           currentDate.getFullYear() === desiredDate.getFullYear();
};


// Usage example:

const date1 = new Date();
console.log(isCurrentDate(date1));  // true

const date2 = new Date('2000-09-10T00:00:00.000Z');
console.log(isCurrentDate(date2));  // false

const date3 = Date.now()
console.log(isCurrentDate(date3));  // true

const date4 = 968544000000 * 1000;  // 2000-09-10 as unix time
console.log(isCurrentDate(date4));  // false

console.log(isCurrentDate('2000-09-10'));  // false
console.log(isCurrentDate(2000, 8, 10));   // false  <---- year, month, day  (month from 0 to 11)

Output:

true
false
true
false
false
false

Alternative titles

  1. Node.js - check if date is today
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join