How to check a date is valid or not using JavaScript ? - GeeksforGeeks

21 Jul, 2021
Given a date object and the task is to check the given date is valid or not using JavaScript. There are two methods to solve this problem which are discussed below:
Approach 1:
Store the date object in a variable.
If the date is valid then the getTime() will always be equal to itself.
If the date is Invalid then the getTime() will return NaN which is not equal to itself.
The isValid() function is used to check the getTime() method is equal to itself or not.
Example 1: This example implements the above approach.
<!DOCTYPE HTML> <html>   <head>     <title>         How to check a date is valid        or not using JavaScript ?    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP" style =        "font-size: 15px; font-weight: bold;">    </p>          <button onclick = "GFG_Fun()">        Click Here    </button>          <p id = "GFG_DOWN" style =        "color:green; font-size: 20px; font-weight: bold;">    </p>          <script>        var up = document.getElementById('GFG_UP');         var down = document.getElementById('GFG_DOWN');         var d = new Date("2012/2/30");                  up.innerHTML = "Click on the button to check"                    + " validity of data.<br>";                  Date.prototype.isValid = function () {                          // If the date object is invalid it            // will return 'NaN' on getTime()             // and NaN is never equal to itself.            return this.getTime() === this.getTime();        };                  function GFG_Fun() {            down.innerHTML = d.isValid();        }     </script> </body>   </html>
Output:
Before clicking on the button:
After clicking on the button:
Approach 2:
Store the date object into a variable d.
Check if the variable d is created by Date object or not by using Object.prototype.toString.call(d) method.
If the date is valid then the getTime() method will always be equal to itself.
If the date is Invalid then the getTime() method will return NaN which is not equal to itself.
In this example, isValid() method is checking if the getTime() is equal to itself or not.
Example 2: This example implements the above approach.
<!DOCTYPE HTML> <html>   <head>     <title>         How to check a date is valid        or not using JavaScript ?    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP" style =        "font-size: 15px; font-weight: bold;">    </p>          <button onclick = "GFG_Fun()">        Click Here    </button>          <p id = "GFG_DOWN" style =        "color:green; font-size: 20px; font-weight: bold;">    </p>          <script>        var up = document.getElementById('GFG_UP');         var down = document.getElementById('GFG_DOWN');         var d = new Date("This is not date.");                  up.innerHTML = "Click on the button to check "                    + "validity of data.<br>";                  function GFG_Fun() {                      if (Object.prototype.toString.call(d)                                    === "[object Date]")            {                if (isNaN(d.getTime())) {                     down.innerHTML = "Invalid Date.";                }                else {                    down.innerHTML = "Valid Date.";                }            }        }     </script> </body>   </html>
Output:
Before clicking on the button:
After clicking on the button:
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
javascript-date
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
How to calculate the number of days between two dates in javascript?
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Top 10 Projects For Beginners To Practice HTML and CSS Skills