Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to check whether an object is a date?

  • Last Updated : 23 May, 2019

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:

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!

object instanceof Date

Example:




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

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:




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

Output:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :