javascripthtmldatetime

How to disable previous date using HTML5 datetime-local input?


I would simply like to prevent users from entering or disable the previous dates in input datetime-local , is there a way to do this?

Here is what I have done, unfortunately nothing happens on this code:

<input type="datetime-local" class="form-control col-md-6" name="book" required>
<script>
  var today = new Date().toISOString().split('T')[0];
  document.getElementsByName('book')[0].setAttribute('min', today);
</script>


Solution

  • try this

    var today = new Date().toISOString().slice(0, 16);
    
    document.getElementsByName("book")[0].min = today;
    <input type="datetime-local" class="form-control col-md-6" name="book" required>