var selectedDateFromCalendar = new Date();
selectedDateFromCalendar = document.getElementById('mydate').value;
You seem to be trying to set the Type of selectedDateFromCalendar, but javascript variables don't have a Type, values do, so just assign values.
The value of a form control is always a string, so even though you initially assigned a Date as the value, selectedDateFromCalendar is now a string.
var diff = selectedDateFromCalendar - currentdate ;
This attempts to subtract one string from another. It "works" where the strings can be converted to numbers (e.g. '5' - '3'
) but not for other types of string.
Since the value returned from the form input is a string, you have to parse it. Do not ever use Date.parse or the Date constructor to parse strings. I can't emphasise that enough, just don't do it.
You should indicate the format of string you want. Then, parse the string manually (a library can help but isn't really necessary) e.g.
// Parse and validate a string in DMY format
function parseDMY(s) {
var b = s.split(/\D/);
var d = new Date(b[2], --b[1], b[0]);
return d && d.getMonth() == b[1]? d : new Date(NaN);
}
// Get the date from a form and subtract from today's date
function dateDiff(idIn, idOut) {
var d = parseDMY(document.getElementById(idIn).value);
var result;
if (isNaN(d)) {
result = 'Invalid date';
} else {
result = d - new Date(); // difference in milliseconds
}
document.getElementById(idOut).textContent = result;
}
<label for="starDate">Start date (dd/mm/yyyy)<input id="startDate" name="startDate" value="25/12/2015"></label>
<br>
<button onclick="dateDiff('startDate','result')">Get date diff</button>
<div id="result"></div>