Check if a Date is valid using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Check if a Date is valid using JavaScript #

To check if a date is valid:

  1. Check if the date is an instance of the Date object.
  2. Check if passing the date to the isNaN() function returns false.
  3. If both conditions are met, the date is valid.
index.js
function dateIsValid(date) { return date instanceof Date && !isNaN(date); } console.log(dateIsValid(new Date('2022-03-36'))); // 👉️ false console.log(dateIsValid(new Date())); // 👉️ true console.log(dateIsValid(new Date('2022-03-24'))); // 👉️ true

We created a reusable function that takes a Date object as a parameter and checks if the date is valid.

The instanceof operator returns a boolean value indicating whether the prototype property of the constructor appears in the prototype chain of the object.

index.js
console.log(new Date() instanceof Date); // 👉️ true console.log({} instanceof Date); // 👉️ false console.log([] instanceof Date); // 👉️ false console.log('hi' instanceof Date); // 👉️ false

The instanceof operator will only return true if the value to the left was created using the Date() constructor or a class that extends it.

The second condition checks if passing the date to the isNaN function returns false.

index.js
function dateIsValid(date) { return date instanceof Date && !isNaN(date); }

The isNaN function takes a value and tries to determine if the value is NaN (not a number).

If the value is not already a number, the isNaN function automatically converts it to one.

When you convert a valid date to a number, you get a timestamp - the number of milliseconds elapsed between the 1st of January and the given date.

index.js
// 👇️ 1643112264226 console.log(Number(new Date())); // 👇️ NaN console.log(Number(new Date('2022-03-36')));

In the second example we passed an invalid date to the Date constructor, so converting the value to a number returned NaN.

If the Date is an instance of the Date() constructor and converting it to a number doesn't give us NaN (not a number) back, then we have a valid Date.

It should be noted that the instanceof operator wouldn't work if the Date object is created in another context, e.g. in an external iframe.

If you are creating the Date object in an external window or an iframe, stringify the object's prototype instead of using the instanceof operator.
index.js
function dateIsValid(date) { return ( Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date) ); } console.log(dateIsValid(new Date('2022-03-36'))); // 👉️ false console.log(dateIsValid(new Date())); // 👉️ true console.log(dateIsValid(new Date('2022-03-24'))); // 👉️ true

The code snippet above should only be used if you are creating your Date objects in an external window, e.g. an iframe.

In all other cases, the instanceof operator gets the job done and is much more readable.

Alternatively, you can use duck-typing.

index.js
function dateIsValid(date) { if ( typeof date === 'object' && date !== null && typeof date.getTime === 'function' && !isNaN(date) ) { return true; } return false; } console.log(dateIsValid(new Date('2022-03-36'))); // 👉️ false console.log(dateIsValid(new Date())); // 👉️ true console.log(dateIsValid(new Date('2022-03-24'))); // 👉️ true console.log(dateIsValid([])); // 👉️ true console.log(dateIsValid({})); // 👉️ true console.log(dateIsValid(null)); // 👉️ true
When using duck-typing, we simply check if the object implements specific properties or methods and if it does, we assume it's an object of the right type.

We first check if the passed in value has a type of object.

Since null also has a type of object in JavaScript, we have to explicitly check that the value is not null.

Then, we check if the object has a getTime property that is a function. You could repeat this process with any other properties or methods that the Date object implements.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.