There are several approaches to check if a date is today’s date or not in JavaScript. However, the easiest approach is to convert both date objects into their respective date strings and then check if both date strings are equal or not.
Approach:
- Create today’s date object using the
Date()
constructor. - Convert the given date string to a date object using the
Date()
constructor. - Convert both date objects to date strings using the
toDateString()
method. - Compare both date strings. If both are equal, the given date is today’s date, otherwise, not.
The following example uses the above approach to check if a date is today’s date or not:
function isTodayDate(date){ // Get today's date const today = new Date(); // Convert both date objects to date strings let todayStr = today.toDateString(); let dateStr = date.toDateString(); // Check if both date strings are equal if(todayStr==dateStr){ return true; }else{ return false; } } console.log(isTodayDate(new Date())); // true console.log(isTodayDate(new Date('2022-10-20'))); // false
In the above example, we first used the Date()
constructor to get the current date. The Date()
constructor returns a date object based on the parameters passed to it.
If no parameters are passed, it returns the current date-time object:
let date = new Date(); console.log(date); // Output: Sun Nov 20 2022 15:43:06 ...
The toDateString()
method converts the given date-time object into a string and returns only the date portion of it.
const date = new Date(); let dateStr = date.toDateString(); console.log(date); // Sun Nov 20 2022 15:49:01 ... console.log(dateStr); // "Sun Nov 20 2022"
Method 2: Using the getFullYear(), getMonth() and getDate() Methods
In this method, we get the year, month, and date from both dates and then check if they are equal. If the values of the year, month and date in both strings are equal, the given date is today’s date else not.
We will make use of the following three functions to get the year, month and date:
Date.getFullYear()
– Returns a 4-digit number representing the year of the specified date.Date.getMonth()
– Returns an integer between 0 and 11 representing the month of the year.Date.getDate()
– Returns an integer between 1 and 31 representing the day of the month.
The following example uses the above three functions to check if a date is today’s date:
function isTodayDate(date){ // Get today's date const today = new Date(); // check if year, month and date are equal if( today.getFullYear()==date.getFullYear() && today.getMonth()==date.getMonth() && today.getDate()==date.getDate() ){ return true; } else{ return false; } } console.log(isTodayDate(new Date())); // true console.log(isTodayDate(new Date('2022-10-20'))); // false
Notice that the Date.getMonth()
method is 0-based i.e. 0 represents January month, 1 represents February month and so on up to 11(December).
Conclusion
In this article, we learned two approaches to check if a date is today’s date.
The first approach is to convert the two dates into their corresponding date strings using the toDateString()
method and then check if the two strings are equal or not. If both strings are equal, the given date is today’s date, otherwise not.
The second approach is to get the year, month and date from both dates using the getFullYear()
, getMonth()
and getDate()
methods and then check if their values are equal or not. If yes, the given date is same as today’s date, otherwise not.