Currently, I am working with Angular 7.
Suppose I have a fixed rate X, for example, an amount I need to pay each month. Now, if I have two specified dates startDate
and endDate
, I want to calculate the total payment due for this given time period. To provide a clearer example:
Let's assume my rate is 10.00
With startDate
as 2018-12-01
and endDate
as 2018-12-31
, I should calculate 1 * 10.00 = 10.00
.
I will only consider full months in this calculation, so an endDate
like 2018-12-13
would not be valid; it has to be either 2018-12-31
, 2019-01-31
, or 2019-02-28
(or 2019-02-29
).
I attempted to solve this but unfortunately could not get it to work, so I did some research and found myself stuck at this function:
monthDiff(d1: Date, d2: Date): number {
let months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
However, even this method did not yield the desired result for me.