Your first condition is always true because of you are not converting string
to Date
. There is two way you can do this.
First, you can construct a Date object with given string and check whether Date is valid or not
const dateString = '2019-12-31T20:30:00+00:00';
const date = new Date(dateString);
if(date.toString() !== "Invalid Date") {
// String is valid date.
} else {
// String is not valid date.
}
The other way is, firstly you can check the given string is valid date and then consruct a Date object.
const dateString = '2019-12-31T20:30:00+00:00';
const parsed = Date.parse(dateString);
if(!isNaN(parsed)) {
// String is valid date.
const date = new Date(parsed);
} else {
// String is not a valid date.
}