The Wayback Machine - https://web.archive.org/web/20240119133354/https://www.geeksforgeeks.org/how-to-check-the-input-date-is-equal-to-todays-date-or-not-using-javascript/
Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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>         

    
    

  • 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>

    
    

  • Output:

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 29 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials