check if a date time string is a valid date in js 1

check if a date time string is a valid date in js

const isValidDate = function(date) {
    return (new Date(date) !== "Invalid Date") && !isNaN(new Date(date));
}

Here is what the above code is Doing:
1. We’re creating a function called isValidDate.
2. We’re passing in a date as a parameter.
3. We’re using the Date constructor to create a new date object.
4. We’re using the !== operator to check if the date is not equal to “Invalid Date”.
5. We’re using the !isNaN() method to check if the date is not equal to NaN.
6. We’re returning the result of the above two checks.

Similar Posts