I have implemented this solution to my school project
However I get this error from the console whenever I attempt to change the value of the initial date field
The specified value "2/21/2023" does not conform to the required format, "yyyy-MM-dd".
What could be wrong in the solution as it works in the example given
I tried change the
dueDateInput.value = dueDate.toLocaleDateString('en-CA');
into
dueDateInput.value = dueDate.toISOString();
and also
dueDateInput.value = dueDate.toLocaleDateString('bo-CN');
but to no avail
Since the browser says that this is the codeline that gives the error, how can I convert the dueDate to YYYY-MM-DD so that I can assign it as the value to the other date field
2
Answers
The issue with
.toISOString()
is that it returns a full date and timestamp, while all you want is the date part. You can usedate.getFullYear()
,date.getMonth()
, anddate.getDate()}
to get the full date. Here’s an example:You can avoid using date strings and simply set the valueAsDate property instead.
This allows you to work directly with
Date
instances which makes date arithmetic much easier.