Here's a sketch of a very simple approach. For simplicity of ideas I will assume that d
, the number of days to add, is positive. It is easy to extend the below to cases where d
is negative.
Either d
is less than 365 or d
is greater than or equal to 365.
If d
is less than 365:
m = 1;
while(d > numberOfDaysInMonth(m, y)) {
d -= numberOfDaysInMonth(m, y);
m++;
}
return date with year = y, month = m, day = d;
If d
is greater than 365:
while(d >= 365) {
d -= 365;
if(isLeapYear(y)) {
d -= 1;
}
y++;
}
// now use the case where d is less than 365
Alternatively, you could express the date in, say, Julian form and then merely add to the Julian form and conver to ymd format.