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.
Check if Input Date is Equal to Today’s Date
JavaScript has a Date class, by creating its instance without any arguments we can get today’s date.
Syntax:
const today = new Date();
Return:
This return a Date object containing the current date and time.
Approach:
After getting the Date object and we can compare it with the input date by using 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;
};
// Check if the current date is today
console.log("Is today? ", isToday(new Date())); // 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, since new Date() without any argument returns 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:
Is today? true
Is today? false
In the above output, you can see that we have got the expected result.
Love JavaScript? Learn more about it here.
Summary
In this tuitorial, we have learned to check if the input date equals today’s date or not by using the JavaScript date object instance. 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.
Reference
https://stackoverflow.com/questions/8215556/how-to-check-if-input-date-is-equal-to-todays-date