How to check the input date is equal to today’s date or not using JavaScript ?

29 Jan, 2020
Given a date object and the task is to determine whether the given date is the same as today’s date or not with the help of JavaScript. There are two well-famous approaches are discussed here.
Approach 1: Get the input date from user (var inpDate) and the today’s date by new Date(). Now, use .setHours() method on both dates by passing parameters of all zeroes. All zeroes are passed to make all hour, min, sec and millisec to 0. 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>        body {            text-align: center;        }        h1{            color: green;        }        #geeks {            color: green;             font-size: 29px;             font-weight: bold;        }    </style></head>   <body >     <h1>GeeksforGeeks</h1>          <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>        var down = document.getElementById('geeks');                  function gfg() {            var date =                 document.getElementById('date').value;                          var inpDate = new Date(date);            var 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>         
<!DOCTYPE HTML> <html>   <head>     <title>         How to Check Input Date is Equal        to Today’s Date or not using        JavaScript?    </title>           <style>        body {            text-align: center;        }        h1{            color: green;        }        #geeks {            color: green;             font-size: 29px;             font-weight: bold;        }    </style></head>   <body >     <h1>GeeksforGeeks</h1>          <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>        var down = document.getElementById('geeks');                  function gfg() {            var date =                 document.getElementById('date').value;                          var inpDate = new Date(date);            var 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: Similarly get the input date from user (var 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>        body {            text-align: center;        }        h1 {            color: green;        }        #geeks {            color: green;             font-size: 29px;             font-weight: bold;        }    </style></head>   <body >     <h1>GeeksforGeeks</h1>          <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>        var down = document.getElementById('geeks');                  function gfg() {            var date =                 document.getElementById('date').value;                          var inpDate = new Date(date);            var 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>
<!DOCTYPE HTML> <html>   <head>     <title>         How to Check Input Date is Equal        to Today’s Date or not using        JavaScript?    </title>           <style>        body {            text-align: center;        }        h1 {            color: green;        }        #geeks {            color: green;             font-size: 29px;             font-weight: bold;        }    </style></head>   <body >     <h1>GeeksforGeeks</h1>          <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>        var down = document.getElementById('geeks');                  function gfg() {            var date =                 document.getElementById('date').value;                          var inpDate = new Date(date);            var 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:
javascript-date
JavaScript-Misc
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Hide or show elements in HTML using display property
How to append HTML code to a div using JavaScript ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?