I was expecting min
and max
for <input>
to limit the selectable options to the range in between the two values. They do in <input type="number">
, for example. But with type="datetime-local"
they don’t. As far as I can tell, they don’t actually do much of anything?
<label for="select_start">Without min/max</label>
<input type="datetime-local" id="select_start">
<label for="select_end">With min/max</label>
<input type="datetime-local" id="select_end" min="2023/5/9" max="2023/6/8">
Both are exactly the same, so far as I can tell (viewing them on Google Chrome in 2023).
Do min
and max
actually do anything? And what can I do to limit the selectable options to the desired range?
2
Answers
The min and max attributes do not work with datetime-local input types. These are generally used with date or numeric input.
Restricting selectable options to a certain range using the datetime-local type requires the use of a custom validation approach in JavaScript.
You understood correctly that you can narrow down the range of the input value using the
min
andmax
attributes. You just provided the date in the wrong format. Since you created a "datetime-local" type input field, the minimum and maximum values need to be in the standard ISO 8601 format:yyyy-mm-ddThh:mm
, such as1970-01-01T23:59
.Example
More information
Global Date(Time) Formats for HTML – MDN
Attributes for <input type="number"> – MDN
Attributes for <input type="date"> – MDN
Attributes for <input type="datetime-local"> – MDN
Warning