If you don't want to do division with possibly large numbers, you can reduce the problem by making Date do some of the work for you
function isLeapYear(year) {
if (year % 4) return false;
if (year % 100) return true;
if (year % 400) return false;
return true;
}
function dayOfYear(date) {
var d1 = Date.UTC(date.getUTCFullYear(), 0, 0),
d2 = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
return (d2 - d1) / 864e5;
}
function daysBetween(start, end) {
var days = dayOfYear(end) - dayOfYear(start);
start = start.getUTCFullYear();
end = end.getUTCFullYear();
for (; start < end; ++start)
days += isLeapYear(start) ? 366 : 365;
return days;
}
Now it is just a case of
daysBetween(new Date(2014, 10-1, 1), new Date(2014, 11-1, 1)); // 32
This is good for if your time range will span decades or centuries
Otherwise just making a quick modification to dayOfYear
gives
function countDays(start, end) {
var d1 = Date.UTC(start.getUTCFullYear(), start.getUTCMonth(), start.getUTCDate()),
d2 = Date.UTC(end.getUTCFullYear(), end.getUTCMonth(), end.getUTCDate());
return (d2 - d1) / 864e5;
}
Then
countDays(new Date(2014, 10-1, 1), new Date(2014, 11-1, 1)); // 32
Please note I used UTC for these calculations, this is just to avoid any daylight savings or other timezone changes