I have many variables - year, month, day. I am trying to convert it to correct date form. I would like to get null if variables form incorrect date
var year = "2023";
var month = "11"
var day = "31"
var date = new Date(year, month - 1, day);
// Log to console
console.log(date)
This code returns 1 December of 2023. But I would like to get null value. How do I do it?
Edit I edited my code. I have dates like 30/11, 29/02, 31/04 which are not valid dates
newalways returns an instance of the class specified, it will never returnnull.Dateobjects automatically handle overflow of dates. If a day number is higher than the number of days in the specified month, it wraps into the next month. So November 31 is not considered an error, it's just another way of saying December 1.