Using JavaScript it is not easy to set the value of an html input of the type datetime-local.
input.valueAsNumber
document.getElementById("dateInput").valueAsNumber = new Date("Thu Mar 30 2023 12:54:17 GMT+0000 (UTC)").valueOf();
The following does not work unless you happen to be in the UTC timezone. valueAsNumber accepts a numeric representation of the datetime but it doesn’t convert the UTC numeric representation to the local timezone.
input.valueAsDate
document.getElementById("dateInput").valueAsDate = new Date("Thu Mar 30 2023 12:54:17 GMT+0000 (UTC)");
The following returns "Uncaught DOMException: Failed to set the ‘valueAsDate’ property on ‘HTMLInputElement’: This input element does not support Date values."
input.value
document.getElementById("dateInput").value = "Thu Mar 30 2023 12:54:17 GMT+0000 (UTC)";
The following returns "The specified value "Thu Mar 30 2023 12:54:17 GMT+0000 (UTC)" does not conform to the required format. The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS""
2
Answers
The following is how this would be accomplished. I posted this question/answer to save someone else the trouble of getting to this solution.
If you are to use the output of Date.prototype.toString, then you should parse the string and create a timestamp that suits your purpose. Only the month name needs to be converted to a number, the rest of what you need is in the string without modification.
See Date.toISOString() but local time instead of UTC to use a Date object directly.
Below is an example of parsing the output of Date.prototype.toString and reformatting it.