2

I have on my site form with input date, which I am now returning as Date - RFC 3339.

I want to have ISO 8601 - value of input date to looks like 2021-12-8T04:30:00-06:00. Convert date RFC 3339 to date in format ISO 8601. Is there some method to do that? Or some library to javascript I can use?

const dateTaskField = document.querySelector('#new-task-date');
dateTaskField.valueAsDate = new Date();
<input type="date" id="new-task-date">
  • While [you can't change the `input` value's format](https://stackoverflow.com/questions/7372038/is-there-any-way-to-change-input-type-date-format) (there is an answer about writing your own [web component](https://developer.mozilla.org/en-US/docs/Web/Web_Components) with the required formatting), you can use [`Date#toISOString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) once you have a value. – D M Dec 07 '21 at 18:36
  • Possibly helpful: [Get ISO string from a date without time zone](https://stackoverflow.com/questions/43670062/get-iso-string-from-a-date-without-time-zone), [How do I output an ISO 8601 formatted string in JavaScript?](https://stackoverflow.com/questions/2573521/how-do-i-output-an-iso-8601-formatted-string-in-javascript), [Javascript date format like ISO but local](https://stackoverflow.com/questions/12413243/javascript-date-format-like-iso-but-local). – D M Dec 07 '21 at 18:39

1 Answers1

1

Solved by assigning it to new Date() function:

const endDateIso = new Date(dateTaskField.value).toISOString();
  • That will produce a date that is ±1 day where users are within their local offset from midnight. – RobG Dec 08 '21 at 03:31