2

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>

2
  • If you want the local date, don't use toISOString as that returns the UTC date, which will be ±1 day for users for the period of their local offset either side of midnight (which can be -10 to +14 hours). See Format JavaScript date as yyyy-mm-dd. Commented Nov 16, 2021 at 5:35
  • The issue is that the value for DateTIme-local must be YYYY-MM-DDTHH:mm. The above trims the time (but it also uses the UTC date and time, not local so that needs to be fixed too). Commented Nov 16, 2021 at 5:47

4 Answers 4

7

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>

Sign up to request clarification or add additional context in comments.

2 Comments

I will try this one, thanks
That gets the current UTC date, not the local date so may be out by ±1 day depending on the current local time and offset.
1

In Laravel, Try this:

 <label>Select date & time:</label><br>
    <input type="datetime-local" name="start_time" min="{{today()}}" value="2024-05-01T00:00" max="2026-01-01T00:00"><br>

Comments

0
        <div class="row">
            <div class="col-md-6">
              <label for="time_start">{{ __('adminstaticword.timestart') }}:<sup class="redstar">*</sup></label>
              <input type="datetime-local" class="form-control" name="time_start" id="time_start" placeholder="Enter time_start" required min="{{date('Y-m-d')}}"  value="{{ old('time_start') }}" >
              <script>
                var today = new Date().toISOString().slice(0, 16);
                document.getElementsByName("time_start")[0].min = today;
                </script>
            </div>   

Comments

0

$(function () {
    var currentday = new Date().toISOString().slice(0, 16); 
        document.getElementsByName("dti")[0].min = currentday;  
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input id="SwapInTime" type="datetime-local" asp-for="SwapInTime" title="Swap In Time" name="dti" />

1 Comment

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.