Alon Alexander Alon Alexander - 1 month ago 34
Javascript Question

JavaScript suppress date format warning

Whenever I try to reset a date input using JavaScript, like so:

//input_id is an id of a date input
document.getElementById(input_id).value = "0000-00-00";


I get a warning in the console:

The specified value "0000-00-00" does not conform to the required format, "yyyy-MM-dd".


Does anyone know a way to suppress this warning so it won't show? The JS keeps on running smoothly but I want to get rid of these warnings.

If you have another way of resetting a date input (without raising a warning) I will be happy to hear.

Thanks in advance.

Answer

You're getting the warning because years, months, and days start at 1, not 0. 0 is an invalid value for all of them. But there's a simpler way to reset an input element..

You can just set the value to the empty string which is the initial default value.

var di = document.createElement("input");
di.type = "date";
console.log(di.value); // outputs ""
document.body.appendChild(di);

// change the value if you want

// to reset:
di.value = "";
Comments