How to get date from input? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

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))) https://code.sololearn.com/WA10a183a22a/#

16th Apr 2021, 7:00 AM
Just@bit$mile
Just@bit$mile - avatar
4 Answers
+ 3
Just@bit$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(); }); }
16th Apr 2021, 9:49 AM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 5
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 - https://code.sololearn.com/c0lN74ozAMBX/?ref=app and please clarify your questions that exactly what do you want from us :)
16th Apr 2021, 9:20 AM
Abhiyantā
Abhiyantā - avatar
+ 5
Based on CamelBeatsSnake solution https://code.sololearn.com/W2eZuvxTBbhc/?ref=app
16th Apr 2021, 10:21 AM
Ipang
+ 2
thanks to all)
16th Apr 2021, 12:33 PM
Just@bit$mile
Just@bit$mile - avatar