[英]Can't set max date on date input
I am trying to set a min and max date to my date inputs, the min setting is working but for some reason the max date is not.我正在尝试为我的日期输入设置最小和最大日期,最小设置有效,但由于某种原因,最大日期无效。 What is the problem here?
这里有什么问题? I have also tried to use getDate()+90 instead of getMonth()+3.
我还尝试使用 getDate()+90 而不是 getMonth()+3。
my inputs:我的输入:
let today = new Date().toISOString().split('T')[0]; let date3m = new Date(new Date().setDate(new Date().getMonth() + 3)); document.getElementsByName("start")[0].setAttribute('min', today); document.getElementsByName("start")[0].setAttribute('max', date3m)
<div class="calendar"> <div> <label for="start">Start Dato:</label> <input type="date" id="start" name="start" required> </div> <div> <label for="end">Slutt Dato:</label> <input type="date" id="end" name="end" required> </div> </div>
You forgot to remove the time from date3m
before setting the input's max
attribute to it.在将输入的
max
属性设置为它之前,您忘记从date3m
删除时间。
let today = new Date().toISOString().split('T')[0]; let date3m = new Date(new Date().setDate(new Date().getMonth() + 3)).toISOString().split('T')[0]; document.getElementsByName("start")[0].setAttribute('min', today); document.getElementsByName("start")[0].setAttribute('max', date3m)
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> <div class="calendar"> <div> <label for="start">Start Dato:</label> <input type="date" id="start" name="start" required> </div> <div> <label for="end">Slutt Dato:</label> <input type="date" id="end" name="end" required> </div> </div>
min
and max
need to be in the format YYYY-MM-DD
. min
和max
需要采用YYYY-MM-DD
格式。 You're setting max
to something like Sat Nov 13 2021 18:14:51 GMT-0500 (Eastern Standard Time)
, which doesn't work.您将
max
设置为Sat Nov 13 2021 18:14:51 GMT-0500 (Eastern Standard Time)
,这不起作用。
Use toISOString()
just like you do for today
.像
today
一样使用toISOString()
。
And setDate()
should be setMonth()
.而
setDate()
应该是setMonth()
。
let today = new Date().toISOString().split('T')[0]; let date3m = new Date(); date3m.setMonth(date3m.getMonth() + 3); date3m = date3m.toISOString().split('T')[0]; document.getElementsByName("start")[0].setAttribute('min', today); document.getElementsByName("start")[0].setAttribute('max', date3m)
<div class="calendar"> <div> <label for="start">Start Dato:</label> <input type="date" id="start" name="start" required> </div> <div> <label for="end">Slutt Dato:</label> <input type="date" id="end" name="end" required> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.