Open In App

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

Last Updated : 29 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
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:


Similar Reads

How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come after some given date(Ex. 'end date' must be after 'start date')
5 min read
How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come before some given date (Ex: 'start date' must come before 'end da
5 min read
jQWidgets jqxCalendar today() Method
jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful and optimized framework, platform-independent, and widely supported. The jqxCalendar represents a jQuery calendar widget that enables the user to select a date using a visual monthly calendar display. The today() method is used to
2 min read
How to validate if input in input field is a valid date using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only a valid date is allowed i.e. there is not allowed any strings, number, or invalid date characters.
5 min read
How to check input file is empty or not using JavaScript/jQuery ?
Given an HTML document containing an input element, the task is to check whether an input element is empty or not with the help of JavaScript.  These are the two approaches to check input file is empty or not using JavaScript/jQuery: Table of Content Using element.files.length property in JavaScriptUsing element.files.length property in jQuery Appr
2 min read
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 e.g."YYYY/DD/MM","DD/MM/YYYY", and "YYYY-MM-DD", etc. we will have a given date format and we need to check whether that given format is valid or not according to the official and acceptable date format. There are two methods to solve this proble
4 min read
React.js Blueprint Date Input Date Formatting
Blueprint is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications. In this article, we'll discuss React.js Blueprint Date Input Date formatting. The Date Input component is a type of InputGroup that allows the users to pick the date as per c
3 min read
Javascript Program for Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
3 min read
JavaScript Program to Check if Two Arrays are Equal or Not
Given two arrays, arr1 and arr2 of equal length N, the task is to find if the given arrays are equal or not. Two arrays are said to be equal if: Both of them contain the same set of elements, Arrangements (or permutations) of elements might/might not be the same.If there are repetitions, then counts of repeated elements must also be the same for tw
3 min read
How to check all values of an array are equal or not in JavaScript ?
To check whether all values of an array are equal or not in JavaScript, we have multiple approaches. Below are the approaches used to check if all values of an array are equal or not in JavaScript: Table of Content Using Array.every() methodUsing Array.reduce() methodUsing SetUsing for...of loopUsing filter() MethodApproach 1: Using Array.every() m
3 min read