How to get date from input? | Sololearn: Learn to code for FREE!


How to get date from input?

Hi Sololearners! I have some problem here. I tried to use getDate() function for input[type=date] but console says that getDate() is not a function. Please help me) //After this I tried input.value.split('-') ; //And it worked. But I wanted to use this function)))

4/16/2021 7:00:06 AM

[email protected]$mile

4 Answers

New Answer


[email protected]$mile One problem with your JavaScript code is that you're not waiting for the the DOM elements to load. You have to wait for that if you're writing your JavaScript outside of the HTML tab. As Rishav says, you need a new Date object. The Date object will give you methods like getDate(). I've fixed your code: document.addEventListener("DOMContentLoaded", handleDateInput); function handleDateInput() { document.querySelector(".button") .addEventListener("click", function () { var year = document.getElementById("year"); var month = document.getElementById('month'); var day = document.getElementById('day'); var inputDate = document.getElementById("date").value; var date = new Date(inputDate); year.innerText = date.getFullYear(); month.innerText = date.getMonth() + 1; day.innerText = date.getDate(); }); }


Hi , Can you please tell me that what you exactly want to do or what is task or problem you are solving!! NOTE - getDate() you can access by creating an object of Date. var b = new Date() console.log(b.getDate()) Will give you current date - and please clarify your questions that exactly what do you want from us :)


Based on CamelBeatsSnake solution


thanks to all)