How do you parse a date from an HTML5 date input?

后端 未结 6 1087
攒了一身酷
攒了一身酷 2020-12-15 23:40

I have an input on my webpage that I am able to set the date on by getting an ISO string and pulling out the first 10 characters.

date = new Date();
dateInpu         


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 00:21

    You can also convert the input string from YYYY-MM-DD to MM/DD/YYYY and it then parse the date to get the correct answer.

    EXAMPLE:

    Don't use:

    new Date(Date.parse('2018-09-28')) // Thu Sep 27 2018 19:00:00 GMT-0500 (Central Daylight Time)
    

    Rather use

    new Date(Date.parse('09/28/2018')) // Fri Sep 28 2018 00:00:00 GMT-0500 (Central Daylight Time)
    

    (NOTE: I am in CDT)

    Tested in Chrome, Firefox, Safari, old Edge, and IE.

提交回复
热议问题