Javascript - get a date from an HTML input

后端 未结 2 1486
一生所求
一生所求 2020-12-31 09:16

I am currently trying to take the value from an HTML input (below)


and put it into a javascr

相关标签:
2条回答
  • 2020-12-31 10:04

    Make sure your script is executing after the DOM has fully loaded. To do this you can put your JS code in a function and call the function once the DOM has loaded, or just put your JS code at the end of your page, right before </body>.

    0 讨论(0)
  • 2020-12-31 10:07
    document.getElementById("dateInput").addEventListener("change", function() {
        var input = this.value;
        var dateEntered = new Date(input);
        console.log(input); //e.g. 2015-11-13
        console.log(dateEntered); //e.g. Fri Nov 13 2015 00:00:00 GMT+0000 (GMT Standard Time)
    });
    

    Basically you need to listen for the change of your date input, currently you were trying to get the value on load, when the picker has no date in it hence you get 'Invalid Date'

    0 讨论(0)
提交回复
热议问题