I used the following function:
function datediff()
{
var dat1 = document.getElementById('date1').value;
alert(dat1);//i get 2010-04-01
var dat2 = document.getElementById('date2').value;
alert(dat2);// i get 2010-04-13
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var diffDays = Math.abs((dat1.getTime() - dat2.getTime())/(oneDay));
alert(diffDays);
}
I get the error:
dat1.getTime()` is not a function
That’s because your
dat1anddat2variables are just strings.You should parse them to get a
Dateobject, for that format I always use the following function:I use this function because the
Date.parse(string)(ornew Date(string)) method is implementation dependent, and the yyyy-MM-dd format will work on modern browser but not on IE, so I prefer doing it manually.