How to Check If a Date Is Today in JavaScript: A Detailed Guide

There might be various reasons to determine if a date is today. Let’s say you’re working on a feature to display exclusive offers that are only available for the present day. In this case, confirming whether a given date is today is crucial. This can also be used while dealing with date inputs or when creating a reminder app where you need to notify the user if a particular task is due today. You can create all these and more related features by checking if a date is today’s date using JavaScript in a few lines of code. So let’s see how.

JavaScript Date Object

Date objects in JavaScript can be created by using its Date class. By creating a Date object without any additional arguments we can get the current date.

Syntax:

const today = new Date();

Return:

This method returns the Date object containing the current day and time.

Example:

// Get today's date
const today = new Date();

// Print today's date
console.log(today);

Output:

JavaScript Date Object

Check if a Date is Equal to the Current Date in JavaScript

Below are the two methods to check if a date is equal to the current date.

1. By comparing the year, month, and date separately

We can use the Date object and compare each parameter such as year, month and date separately with the input date. For this, we can make use of the getDate(), getMonth() and getFullYear() methods, which return the day, month and year respectively. By comparing each parameter of both dates we can validate whether the given date is today’s date or not. 

If this sounds confusing, let’s look at a code example that will solve each of your problems.

Example:

const isToday = (dateToCheck) => {
    // Get today's date
    const today = new Date();

    // Compare the components of the dateToCheck with today's date
    const isSameDate =
        dateToCheck.getDate() === today.getDate() &&
        dateToCheck.getMonth() === today.getMonth() &&
        dateToCheck.getFullYear() === today.getFullYear();

    // Return true if the dateToCheck is today, otherwise return false
    return isSameDate;
};

console.log("Checking if a date is today by comparing the year, month, and date separately: ")

// Check if the current date is today
console.log("Is today? ", isToday(new Date('2024-01-18'))); // true

// Check if a specific date (October 13, 2023) is today
console.log("Is today? ", isToday(new Date('10-13-2023'))); // false

Here we have created a function isToday() which takes a date as an argument. This function gets today’s date using new Date() and compares it with the passed date, one by one each parameter like date, month, and year and if everything matches means that the input date is equal to today’s date and it returns true, otherwise, it returns false.

Later we used the JS console.log() method which takes string and method to log and passed isToday() function with new Date() as an argument with today’s date, so this function should return true.

Next, we passed a random date inside the new Date() and we got false as the return value because the date passed is not today’s date.

Output:

By comparing the year, month, and date separately

In the above output, you can see that we have got the expected result.

2. Using the toDateString() Method

The above method is an easy way but it requires a lot of code. If you want to check faster and do not want to compare every factor of a Date then you can use the JavaScript toDateString() method.

The toDateString() method can convert a Date object (having both date and time) into a string containing only the date part and not the time component so that we can compare whether the given date is today or not.

Example:

function isToday(dateToCheck) {
    // Get today's date
    const today = new Date();

    // Convert both dates to strings in the format "Day Mon DD YYYY"
    const todayString = today.toDateString();
    const dateString = dateToCheck.toDateString();

    // Return true if the dateToCheck is today, otherwise return false 
    return todayString === dateString;
}

console.log("Checking if a date is today by using the toDateString() Method: ")

// Check if the current date is today
console.log("Is today? ", isToday(new Date('2024-01-18'))); // true

// Check if a specific date (October 13, 2023) is today
console.log("Is today? ", isToday(new Date('2023-10-13'))); // false

Output:

Using the toDateString() Method

Love JavaScript? Learn more about it here.

Summary

In this article, we have learned to get the current date using the JavaScript Date object instance and check if input date is equal to it or not. We have also understood why it is necessary to look into it.

If you want a more profound knowledge of the JavaScript Date object, we recommend you to read our tutorial on JavaScript Date Object (with Real-Time Clock Application). At last, we hope you have liked the approach and the content of this article.

Also Read: Get the Current Timestamp in Node.js

Reference

https://stackoverflow.com/questions/8215556/how-to-check-if-input-date-is-equal-to-todays-date

Aditya Gupta
Aditya Gupta
Articles: 109