javascript - React JSX add min date time to datetime-local input - Stack Overflow

I am using React and JSX to return an input field of type "datetime-local". I want the min va

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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

The 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" />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745467662a4628985.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信