Solution Review: Validate the Date

This lesson will explain the solution to the problem in the previous lesson.

We'll cover the following...

Solution #

Press + to interact
const isValidDate = dateString => {
const date = new Date(dateString);
if (
Object.prototype.toString.call(date) === "[object Date]" &&
!isNaN(date.getTime())
) {
return true;
} else {
return false;
}
};
console.log(isValidDate("foo"));
console.log(isValidDate("October 30, 2019"));
console.log(isValidDate("May 8, 2016 10:12:00"));
console.log(isValidDate("April 15, 2012 11:xyz"));

Explanation

In this challenge, you need to check whether the dateString that we pass is valid or not. The requirement was to check for two things:

  • Whether date is a valid date object

  • Whether the time in ...