htmldatetime-local

Preselect value for minutes in datetime-local HTML


I need to select date from datetime-local, but I want the minutes to be preselected as '00'

Like: mm/dd/yyyy --:--:00

So that if the user selects a date-time, minutes will always be set to 00.

My HTML code is: <input type="datetime-local" name="datetime_regular_start" id="datetime_regular_start" value="" required>

I have tried

const dateControl = document.querySelector('input[type="datetime-local"]');
dateControl.value = "2020-06-01T08:30";

And the the whole date-time value is preselected,

So I have tried changing only the minutes to 00.

const dateControl = document.querySelector('input[type="datetime-local"]');
dateControl.value = "yyyy-mm-dd-HH:00";

But Its not working. How should I set the value of minutes to always 00?


Solution

  • You can set the step attribute to 3600 seconds (i.e. one hour):

    <input type="datetime-local" step="3600">

    Browser support might vary. It works in the current version of Chrome, but not in Firefox.