How to make the <input type="datetime-local" >
have the maximum date and time value, today. Through JS.
It is necessary that when choosing the date and time of the user, there should be a limit to today and the time of opening the modal window.
have the maximum date and time value, today. Through JS. It is...">
How to make the <input type="datetime-local" >
have the maximum date and time value, today. Through JS.
It is necessary that when choosing the date and time of the user, there should be a limit to today and the time of opening the modal window.
Purely for date, use date
:
var today = new Date(); var dd = today.getDate(); var mm = today.getMonth() + 1; // Месяца идут с 0, так что добавляем 1. var yyyy = today.getFullYear(); if(dd < 10){ dd='0' + dd } if(mm < 10){ mm='0' + mm } today = yyyy + '-' + mm + '-' + dd; document.getElementById("datetime-local").setAttribute("max", today);
<input id="datetime-local" type="date"></input>
If you need more time, then you can like this:
var today = new Date(); var dd = today.getDate(); var mm = today.getMonth() + 1; // Месяца идут с 0, так что добавляем 1. var yyyy = today.getFullYear(); var minutes = today.getMinutes(); var hour = today.getHours(); if(dd < 10){ dd='0' + dd } if(mm < 10){ mm='0' + mm } if(hour < 10){ hour='0' + hour } if(minutes < 10){ minutes='0' + minutes } today = yyyy + '-' + mm + '-' + dd + 'T' + hour + ':' + minutes; document.getElementById("datetime-local").setAttribute("max", today);
<input id="datetime-local" type="datetime-local"></input>
<input type="datetime-local" id="meeting-time" name="meeting-time" value="2018-06-12T19:30" min="2018-06-07T00:00" max="2018-06-14T00:00">
max - the maximum date that can be set by the user
link to more information:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local
Source: https://ru.stackoverflow.com/questions/967951/
All Articles