EN
JavaScript - check if date is today
3
points
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