-2

I am trying to get date from input and print only date on but something is wrong with my code Please help me to rectify it.

Here is the code:

var input = document.getElementById("dateInput").value; 
var d = new Date(input);
document.getElementById("demo").innerHTML = d.getDate();
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript get Date</h2>
<input type="date" id="dateInput" />

<p>The Date is:</p>

<p id="demo"></p>
</body>
</html>

2

2 Answers 2

0

You need add an event listener to listen event of input change:

document.getElementById("dateInput").onchange = function(){
    var input = this.value; 
    var d = new Date(input);
    document.getElementById("demo").innerHTML = d.getDate();
};
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript get Date</h2>
<input type="date" id="dateInput" />

<p>The Date is:</p>

<p id="demo"></p>
</body>
</html>

See onchange Event - W3C School

Sign up to request clarification or add additional context in comments.

Comments

0

You can use also jqueryui datepicker as this date field won't support across all browsers

document.getElementById("dateInput").addEventListener("change", function() {
    var input = this.value;
    var dateEntered = new Date(input);
    console.log(input); 
    console.log(dateEntered); // GMT+0000 (GMT Standard Time)
});
<input type="date" id="dateInput">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.