How to check whether an object is a date?

Method 1: Using instanceof operator: The instanceof operator checks whether the prototype property of a constructor appears anywhere in the prototype chain of an object. In this case, it is used to check whether the object is an instance of the Date object or not. A true value means that it does match the object specified.
The validity of the date in the Date object can be checked with the !isNan() function. It returns true if the date is not invalid.

Syntax:

object instanceof Date

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to check an object is
        date in JavaScript?
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        How to check if a object is
        date in JavaScript?
    </b>
      
    <p>
        Click on the button to check
        if an object is a date
    </p>
      
    <p>
        Output for string: 
        <span class="outputString"></span>
    </p>
      
    <p>
        Output for number: 
        <span class="outputNumber"></span>
    </p>
      
    <p>
        Output for date: 
        <span class="outputDate"></span>
    </p>
  
    <button onclick="checkDate()">
        Click here
    </button>
      
    <script type="text/javascript">
  
        function checkDate() {
            let str = new String('This is a string');
            let num = new Number(25);
            let date = new Date('13-January-19');
  
            ans = (str instanceof Date) && !isNaN(str);
            document.querySelector('.outputString').textContent = ans;
              
            ans = (num instanceof Date) && !isNaN(num);
            document.querySelector('.outputNumber').textContent = ans;
              
            ans = (date instanceof Date) && !isNaN(date);
            document.querySelector('.outputDate').textContent = ans;
        }
    </script>
</body>
</html>                    

chevron_right


Output:

  • Before clicking the button:
    instanceof-before
  • After clicking the button:
    instanceof-after

Method 2: Using Object.prototype.toString.call() Method: The Object.prototype.toString.call() method is used to return the internal class property of an object in a string of the format ‘[object Type]’. This property is assigned internally during the creation of any object. This property can be checked for the Date object by comparing it with the string ‘[object Date]’. A true value means that it does match the object specified.
The validity of the date in the Date object can be checked with the !isNan() function. It returns true if the date is not invalid.

Syntax:

Object.prototype.toString.call(object)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        How to check if a object
        is date in JavaScript?
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        How to check if a object
        is date in JavaScript?
    </b>
      
    <p>
        Click on the button to check
        if an object is a date
    </p>
      
    <p>
        Output for string: 
        <span class="outputString"></span>
    </p>
      
    <p>
        Output for number: 
        <span class="outputNumber"></span>
    </p>
      
    <p>
        Output for date: 
        <span class="outputDate"></span>
    </p>
  
    <button onclick="checkDate()">
        Click here
    </button>
      
    <script type="text/javascript">
  
        function checkDate() {
            let str = new String('This is a string');
            let num = new Number(25);
            let date = new Date('13-January-19');
  
            ans = Object.prototype.toString.call(str)
                    === '[object Date]' && !isNaN(str);
            document.querySelector('.outputString').textContent
                    = ans;
                      
            ans = Object.prototype.toString.call(num)
                    === '[object Date]' && !isNaN(num);
            document.querySelector('.outputNumber').textContent
                    = ans;
                      
            ans = Object.prototype.toString.call(date)
                    === '[object Date]' && !isNaN(date);
            document.querySelector('.outputDate').textContent
                    = ans;
        }
    </script>
</body>
  
</html>                    

chevron_right


Output:

  • Before clicking the button:
    toString-before
  • After clicking the button:
    toString-after


My Personal Notes arrow_drop_up

Technical Writer Lets have an update

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.