HTML5 Datetime-Local picker unable to alter default value

Im using the HTML5 datetime-local picker and have some custom JS to set the default date/time for me however when trying to use the picker to change the date or time, it doesn’t update the input that contains the original value from JS.

Any ideas?

HTML:

<input id="dt" class="input" type="datetime-local">

JS:

  let dateTime = new Date().toISOString();
  dateTime = dateTime.split(".")[0];
  document.getElementById("dt").value = dateTime;

TIA

9 thoughts on “HTML5 Datetime-Local picker unable to alter default value”

  1. So it seems to be a little bug when by default JS inserted ‘seconds’ into the datetime-picker.

    By removing the seconds from new Date().toISOString() it has now allowed me to freely use the picker to set the date/time and it will update the value.

      let dateTime = new Date().toISOString();
      dateTime = dateTime.split(".")[0];
      dateTime = dateTime.slice(0, -3);
      document.getElementById("dt").value = dateTime;
    
    Reply
  2. It seems to works…
    Are you sure it’s the only html element with that id?

      let dateTime = new Date().toISOString();
      dateTime = dateTime.split(".")[0];
      document.getElementById("dt").value = dateTime;
    <input id="dt" class="input" type="datetime-local">
    Reply

Leave a Comment