Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :