I am currently faced with the challenge of determining the number of effective working months within given date ranges. This involves calculating the ratio of working days to total days in each month, excluding weekends.
effective working month = Number of working days work per month / total working days of that month
working days are defined as days without weekends.
The current algorithm being used is:
Total working months = number of working months in first month + number of working months in last month + number of months in between
This approach has helped simplify calculations without the need for loops. However, the code is lengthy and contains multiple functions. Unfortunately, I cannot share the code due to data sensitivity. I would appreciate any insights into a more efficient algorithm to optimize performance.
//Unit test with start date = 2020-05-20, end date = 2021-08-11, expected result of 14.74458875
Number of months in first month = workingDays(2020-05-20,2020-05-31) / workingdays(2020-05-01,2020-05-31)
= 0.380952381
Number of months in last month = workingDays(2021-08-01,2021-08-11) / workingdays(2021-08-01,2021-08-31)
= 0.363636364
Months in between = 14
Total months = 0.380952381 + 14 + 0.363636364
= 14.74458875