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

后端 未结 6 1081
攒了一身酷
攒了一身酷 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条回答
  •  萌比男神i
    2020-12-15 23:56

    Try using .toLocaleString to get the date into the input, and add the time when parsing it, like this:

    date = new Date();
    dateInput.value = date.toLocaleString("sv-SE", {
        hour: "2-digit",
        minute: "2-digit",
        second: "2-digit"
    });
    

    And to get the value into a Date object use:

    new Date(inputdateInput.value + "T00:00");
    

    I wrote a little post about converting between Date objects and date inputs here

提交回复
热议问题