I need to find number of days between 2 selected dates. I have tried the following code but it returns error results for some dates.
function getDateObject(str) {
var arr = str.split("-");
return new Date(arr[0], arr[1], arr[2]);
}
$('#from_date,#to_date').bind("change paste keyup", function() {
var date1 = getDateObject($('#from_date').val());
var date2 = getDateObject($('#to_date').val());
var days = (date2 - date1) / 86400000;
alert(days); //it returns -1 for from:2016-01-31 - to:2016-02-01.
//other dates like from:2016-01-13 - to:2016-01-14 returns 1 correctly
}
please help me to resolve this issue.