Home>

We become indebted to.
A programmer who is a fledgling programmer who is self-taught in JavaScript.

When I run the following program and compare Date types, some mysterious results are output.

var date1 = new Date ("2020-11-25");
var date2 = new Date ("2020-11-24");
// Error output if date1 is date2 or higher
if (date1>= date2) {
Alert ("date input error");
};


Dates are both date1 and date2If the unit of month is 2 digits, it behaves as expected.However, if the unit of the month is one digit, it does not seem to correspond to the if statement that catches the error and it will pass through.
No error was output even if both months or either month was a single digit.

[2020.11.24 --Addition]

■ Conditions for outputting an error
If date1 is a date greater than or equal to date2, an error alert will be output.

■ Date pattern where an error is output
date1 = new Date ("2020-11-25" + "T00:00:00")
date2 = new Date ("2020-11-24" + "T00:00:00")

//Console.log output value
date1 = Wed Nov 25 2020 00:00:00 GMT + 0900 (Japan Standard Time)
date2 = Tue Nov 24 2020 00:00:00 GMT + 0900 (Japan Standard Time)

■ Date pattern with no error output
date1 = new Date ("2020-9-25" + "T00:00:00")
date2 = new Date ("2020-9-24" + "T00:00:00")

//Console.log output value
date1 = Invalid Date
date2 = Invalid Date

I noticed that when the month is a single digit, the date is not output as an error because the variable does not recognize it as a date.
* I'm ashamed to know how to use Console.log.
Why is it not recognized as a date?
I would like advice from all the experts.

■ Operating environment
・ Windows 10
・ XAMPP Ver7.4.1 (latest)