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

后端 未结 6 1076
攒了一身酷
攒了一身酷 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-15 23:55

    as said by Marty the problem is the difference between the representation of the timezone of the input (UTC defined by W3C) and the JS timezone (local). The solution is to getTimezoneOffset (which is in minutes) and convert everything to milliseconds:

    var today = document.getElementById("myInputDate").valueAsDate;
    var tomorrow = new Date(today.valueOf() + 86400000 + (today.getTimezoneOffset() * 60000));
    

    86400000 = milliseconds of a day

    today.getTimezoneOffset() * 60000 = timezoneOffset in milliseconds

提交回复
热议问题