How to Check the Input Date is Equal to Today's Date or not using JavaScript?
Last Updated :
14 Oct, 2024
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.
HTML
<!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.
HTML
<!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:
Similar Reads
How to check a date is valid or not using JavaScript? To check if a date is valid or not in JavaScript, we have to know all the valid formats of the date. For ex - "YYYY/DD/MM", "DD/MM/YYYY", and "YYYY-MM-DD", etc. We have a given date format and we need to check whether the given format is valid or not according to the official and acceptable date for
3 min read
How to check if date is less than 1 hour ago using JavaScript ? Given a date and the task is to check whether the given date is less than 1 hour ago or not with the help of JavaScript. Approach 1: Count the milliseconds of the difference between the current and prev_date.If those are greater than milliseconds in 1 hour, then it returns false otherwise returns tr
2 min read
How to calculate the yesterday's date in JavaScript ? In this article, we will see how to calculate yesterdayâs date in JavaScript. To calculate yesterday's date, you need to have some basic ideas of a few methods of JavaScript. JavaScript getDate() MethodJavaScript setDate() MethodJavaScript getDate() Method: It is an inbuilt JavaScript function that
3 min read
Check If a Date is Today or in the Future in Moment JS Moment.js is the JavaScript library that is capable of parsing, validating, manipulating, and formatting dates and times. It provides a range of methods to perform date comparisons, such as checking if a date is today or in the future. Using methods like isSame, isAfter, isBefore, and diff, you can
3 min read
How to get tomorrow's date in a string format in JavaScript ? In this article, we will see how to print tomorrow's date in string representation using JavaScript. To achieve this, we use the Date object and create an instance of it. After that by using the setDate() method, we increase one date to the present date. Now by using the getDate() method you will ge
2 min read