1

I am working on getting date difference I have a formula to calculate it as follows

365*year + year/4 - year/100 + year/400 + date + (153*month+8)/5

but its not working on leap years like

if dates are 1-1-2012 and 1-1-2013 the difference should be 366 days but its giving 365.

where am I wrong?

Willie Wong
  • 75,276

1 Answers1

1

This answer tells how to convert from the Gregorian Calendar to the Julian Day Number. To get the number of days between two dates, just subtract the Julian Day Numbers. Note that January and February are counted as the end of the previous year. That should help with the leap years.

$1\text{ Jan }2012\mapsto(2011,10,1):$ $$ 365\times2011+\left\lfloor\frac{2011}{4}\right\rfloor-\left\lfloor\frac{2011}{100}\right\rfloor+\left\lfloor\frac{2011}{400}\right\rfloor+\left\lfloor\frac{153\times10+2}{5}\right\rfloor+1721119\\[18pt] 734015+502-20+5+306+1721119=2455927 $$ $1\text{ Jan }2013\mapsto(2012,10,1):$ $$ 365\times2012+\left\lfloor\frac{2012}{4}\right\rfloor-\left\lfloor\frac{2012}{100}\right\rfloor+\left\lfloor\frac{2012}{400}\right\rfloor+\left\lfloor\frac{153\times10+2}{5}\right\rfloor+1721119\\[18pt] 734380+503-20+5+306+1721119=2456293 $$ and $2456293-2455927=366$.

robjohn
  • 353,833