'Calculating the end date for a period of time

How can I calculate the end date for a given period of time, based on a start date, today's date, and the definition of "period of time"?

Example 1:

A tenant pays rent every week (ie. "period of time" = 7 days). He stops paying rent and has paid up until 2022-01-07, rent is due on 2022-01-08 (and every 7 days after that too).

This means rent is due on 2022-01-08 for the period up to and including 2022-01-14. The next period is 2022-01-15 to 2022-01-21. The one after that is 2022-01-22 to 2022-01-28 etc.

Today's date is 2022-01-16, this means the current rent period end date is 2022-01-21.

Similar to the weekly rent period, there is daily and fortnightly. However, there are also monthly, quarterly and yearly which do not have a defined number of days as it depends on the number of days in the months.

Example 2:

The tenant has paid to 2022-01-05. Today's date is 2022-07-02. The rent period is quarterly.

First rent period: 2022-01-06 -> 2022-04-05

Second rent period: 2022-04-06 -> 2022-07-05 etc

Today's date falls in the 2nd rent period, so the answer is 2022-07-05.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source