How to Check the Input Date is Equal to Today's Date or not using JavaScript?
To check if an input date is equal to today's date in JavaScript, you can compare the input date with the current date by creating Date objects for both and checking if they match. This comparison typically involves comparing the year, month, and day values of both dates.
Below are the approaches to check the input date is equal to today's date or not:
Approach 1: Using setHours() method
Get the input date from the user (let inpDate) and today's date by new Date(). Now, use.setHours() method on both dates by passing the parameters of all zeroes. All zeroes are passed to make all hour, min, sec, and millisec to 0. Now compare today's date with the given date and display the result.
Example: This example implements the above approach.
<!DOCTYPE HTML>
<html>
<head>
<title>
How to Check Input Date is Equal
to Today’s Date or not using
JavaScript?
</title>
<style>
#geeks {
color: green;
font-size: 29px;
font-weight: bold;
}
</style>
</head>
<body>
<b>
Type the date in given format
and <br>check if it is same as
today's date or not.
</b>
<br><br>
Type date: <input id="date" placeholder="mm/dd/yyyy" />
<br><br>
<button onclick="gfg();">
click here
</button>
<p id="geeks"></p>
<script>
let down = document.getElementById('geeks');
function gfg() {
let date =
document.getElementById('date').value;
let inpDate = new Date(date);
let currDate = new Date();
if (inpDate.setHours(0, 0, 0, 0) ==
currDate.setHours(0, 0, 0, 0)) {
down.innerHTML =
"The input date is today's date";
}
else {
down.innerHTML = "The input date is"
+ " different from today's date";
}
}
</script>
</body>
</html>
Output:

Approach 2: Using toDateString() method
Get the input date from user (let inpDate) and the today's date by using new Date(). Now, we will use .toDateString() method
on both dates to convert them to readable strings. Now compare today's date with given date and display the result.
Example: This example implements the above approach.
<!DOCTYPE HTML>
<html>
<head>
<title>
How to Check Input Date is Equal
to Today’s Date or not using
JavaScript?
</title>
<style>
#geeks {
color: green;
font-size: 29px;
font-weight: bold;
}
</style>
</head>
<body>
<b>
Type the date in given format
and <br>check if it is same as
today's date or not.
</b>
<br><br>
Type date: <input id="date" placeholder="mm/dd/yyyy" />
<br><br>
<button onclick="gfg();">
click here
</button>
<p id="geeks"></p>
<script>
let down = document.getElementById('geeks');
function gfg() {
let date =
document.getElementById('date').value;
let inpDate = new Date(date);
let currDate = new Date();
if (currDate.toDateString() ==
inpDate.toDateString()) {
down.innerHTML =
"The input date is today's date";
}
else {
down.innerHTML = "The input date is"
+ " different from today's date";
}
}
</script>
</body>
</html>
Output:
