Can l get a real date from input type=date not a string?

Viewed 70

l was wondering can l get a date type from a input type=date,because when l get value of this input in js l got string type,so i don't want that course l wanna do some arithmetic operators on this date(example:l want to get date from a input control and after in js to get date time now,so l can take away input date from current date and find a age of a person).So how l can do that?

2 Answers

You can use the input's valueAsDate property

document.getElementById('tgt').addEventListener('change', function() {
    console.log(Object.prototype.toString.call(this.valueAsDate));
});
<input type="date" id="tgt">

Alternatively, other than valueAsDate mentioned in Bravo's answer, you can pass it to Date.

let strValue = document.getElementById("myDate").value; 
let dateValue = new Date(strValue);
Related