typescript how to check if string is a date 1

typescript how to check if string is a date

var isDate = 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 isDate.
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 checking if the date is valid.
5. We’re checking if the date is not a number.
6. We’re returning true if the date is valid and not a number.
7. We’re returning false if the date is invalid or a number.

Similar Posts