If I understood your problem correctly, it could be solved using the principle of exclusion of inclusion .
Let's say if your start date is somewhere in 2008 and the end date is in 2010:
NDAYS(start, end) * coeff_2008 - NDAYS(2009, end) * coeff_2008 + NDAYS(2009, end) * coeff_2009 - NDAYS(2010, end) * coeff_2009 + NDAYS(2010, end) * coeff_2010
Where Ndays calculates the number of dates in an interval (TotalDays plus one day).
There is no need to handle leap years on purpose or calculate December 31. Details that you can develop in a cycle passing through jan first of each year in the interval.
source share