admin管理员组文章数量:1426091
I am using React and JSX to return an input field of type "datetime-local". I want the min value to be the date we have today with the current time. I do not know how to write this however.
What I tried :
<input min = {new Date().toLocaleString()} id = "date" type = "datetime-local"/>
which did not work and no min value is set when I choose datetime .
I am using React and JSX to return an input field of type "datetime-local". I want the min value to be the date we have today with the current time. I do not know how to write this however.
What I tried :
<input min = {new Date().toLocaleString()} id = "date" type = "datetime-local"/>
which did not work and no min value is set when I choose datetime .
Share Improve this question edited May 6, 2021 at 17:55 T J 43.2k13 gold badges86 silver badges142 bronze badges asked May 6, 2021 at 17:20 Βαρβάρα ΞιάρχουΒαρβάρα Ξιάρχου 991 silver badge10 bronze badges1 Answer
Reset to default 4The min
, max
values needs the format: yyyy-MM-ddThh:mm
.
new Date().toISOString()
returns a string which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ
or ±YYYYYY-MM-DDTHH:mm:ss.sssZ
, respectively.
We can then use string manipulation methods to get the format we want, without the seconds and milliseconds bit :ss.sssZ
, for example using String.slice()
:
<input
min={new Date().toISOString().slice(0, -8)}
id="date" type="datetime-local" />
本文标签: javascriptReact JSX add min date time to datetimelocal inputStack Overflow
版权声明:本文标题:javascript - React JSX add min date time to datetime-local input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745467662a2659601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论