I want to calculate the number of days between 2 dates. Common problem.
For exemple :
var d0 = new Date("2016-02-27");
var d1 = new Date("2017-08-25");
Many people recommend using epoch difference :
var res = (d1 - d0) / 1000 / 60 / 60 / 24;
// res = 545 days
But I was doubtful so I wrote a naive function :
function days(d0, d1)
{
var d = new Date(d0);
var n = 0;
while(d < d1)
{
d.setDate(d.getDate() + 1);
n++;
}
return n;
}
This function and epoch difference essentially output the same result but not with my particuliar exemple. Probably because 2016 is a leap year.
res = days(d0, d1);
// res = 546 days
Any idea why ?