为本地日期时间设置限制,仅选择当前日期



我现在正在编写一个包含日期选择器的表单。。我正在尝试使用这个元素。现在,我想限制它只选择当前日期。

例如今天是7月7日

因此日期选择器上的选择将仅为7月7日

有人能帮我吗?我是网络应用的新手

<div class="form-group ">
<label class="font-weight-bold">Check in</label>
<input type="datetime-local" id="datefield">
</div>
<div class="form-group ">
<label class="font-weight-bold">Check Out</label>
<input type="datetime-local" id="CheckOut">
</div>

<div class="form-group ">
<label class="font-weight-bold">Check in</label>
<input type="datetime-local" id="CheckIn" min="2021-07-07T00:00" max="2021-07-07T23:59" >
</div>
<div class="form-group ">
<label class="font-weight-bold">Check Out</label>
<input type="datetime-local" id="CheckOut" min="2021-07-07T00:00" max="2021-07-07T23:59">
</div>

或者,如果你想要";当天";您将需要一些Javascript:

const today=(new Date()).toLocaleString("EN-CA").slice(0,10); // get local current date
document.querySelectorAll('input[type="datetime-local"]').forEach(el=>{
el.min=today+"T00:00"; el.max=today+"T23:59";
})
<div class="form-group ">
<label class="font-weight-bold">Check in</label>
<input type="datetime-local" id="CheckIn">
</div>
<div class="form-group ">
<label class="font-weight-bold">Check Out</label>
<input type="datetime-local" id="CheckOut">
</div>

我在这里使用Date.prototype.toLocaleString()而不是Date.prototype.toISOString(),因为这将返回区域设置日期,而不是GMT日期,这可能在一天中的某些时间有所不同,这取决于用户所在的时区;EN-CA";确保";YYYY-MM-DD";总体安排

最新更新