I have to simply calculate difference between two dates, and display it as integer but my code below gives errors if there are large amount of dates (more than 26) in between 2 dates, as well as if there is a date "31st" of a month in between 2 dates.
Can not find whats wrong with my code...?
The values of 'ires_sakuma_datums'
and 'ires_beigu_datums'
are set by jquery calendar picker and are displayed in the format e.g. 25-08-2012
and 17-09-2012
respectively and the result should be displayed into id 'dienu_skaits'
Example 'ires_sakuma_datums'
is set to 28-08-2012
and 'ires_beigu_datums'
is set to 29-09-2012
and it results into 31.041666666666668
although I would expect to result into 32
function getDays()
{
var x = document.getElementById('ires_sakuma_datums').value;
var y = document.getElementById('ires_beigu_datums').value;
//assuming that the delimiter for dt time picker is a '-'.
var arr1 = x.split('-');
var arr2 = y.split('-');
var dt1 = new Date();
dt1.setFullYear(arr1[2], arr1[1], arr1[0]);
var dt2 = new Date();
dt2.setFullYear(arr2[2], arr2[1], arr2[0]);
document.getElementById('dienu_skaits').value = (dt2.valueOf() - dt1.valueOf()) / (60 * 60 * 24 * 1000);
document.forms['test'].elements['dienu_skaits'].focus();
}