Date validation in JavaScript

instanceof

 

The instanceof operator verifies to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value. To check if a date is valid or not we will create date object using date string if new object returns a proper date then will be valid.
 


function isValidDate(dateObj) {
  return !isNaN(date) && dateObj instanceof Date;
}

console.log(isValidDate(new Date('2020-08-48')));
// false

console.log(isValidDate('2020-08-12')); 
// true

console.log(isValidDate(new Date('2020-11-45')));
// false

Tags:

Share:

Related posts