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$.