Unable to define datetime-local minimum time for current date (HTML&JavaScript/beginner)
Go to solution
Solved by RockSolid1106,
Here is my code:
Spoiler
<input type= "datetime-local" name="datefield" id="datefield" min="today_min" max="today_max"> <span class="validity"></span> <br> </label> <br> <input type= "datetime-local" name="datefield2" id="datefield2" min="today_min2" max="today_max2"> <script> var today = new Date(); var dd = today.getDate(); var mm = today.getMonth() + 1; var yyyy = today.getFullYear(); // var hh = today.getHours(); var m = today.getMinutes(); if (dd < 10) { dd = '0' + dd; } if (mm < 10) { mm = '0' + mm; } today_min = `${yyyy}-${mm}-${dd}T10:00`; today_max = `${yyyy}-${mm}-${dd}T10:30`; today_min2 = `${yyyy}-${mm}-${dd}T15:01`; today_max2 = `${yyyy}-${mm}-${dd}T15:30`; //or Year-Month-Day document.getElementById("datefield").setAttribute("min", today_min); document.getElementById("datefield").setAttribute("max", today_max); document.getElementById("datefield2").setAttribute("min", today_min2); document.getElementById("datefield2").setAttribute("max", today_max2); </script>
And it works as expected:
Spoiler
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now