The WorkingDaysCalculator is a demonstration of calculating the number of working days between two days, assuming the weekend falls on Saturday and Sunday, using the following algorithm:
- If the start date is a Sunday, move it back to the Saturday of the same weekend
- If the start date is not a Saturday, roll it forward to the next Saturday, counting the number of working days required, fs
- If the end date is not a Saturday, roll it forward to the next Saturday, counting the number of working days required, fe
- Calculate the number of working days between the adjusted start and end dates
- Divide by 7 to give the number of complete weeks, w
- Calculate the number of working days as 5 * w + fs - fe - 1
The following code snippet illustrates the use of the calculator to calculate the working days between two dates accounting for 2020 public holidays in England:
DateTime[] bankHolidays =
{
new DateTime(2020, 1, 1),
new DateTime(2020, 4, 10),
new DateTime(2020, 4, 13),
new DateTime(2020, 5, 8),
new DateTime(2020, 5, 25),
new DateTime(2020, 8, 31),
new DateTime(2020, 12, 25),
new DateTime(2020, 12, 28)
};
DateTime start = new DateTime(2020, 1, 24);
DateTime end = new DateTime(2020, 4, 24);
WorkingDaysCalculator calculator = new WorkingDaysCalculator();
int days = calculator.Calculate(start, end, bankHolidays);
Console.WriteLine($"There are {days} working days between {start.ToString("dd/MMM/yyyy")} and {end.ToString("dd/MMM/yyyy")}");
- Dave Walker - Initial work
To file issues or suggestions, please use the Issues page for this project on GitHub.
This project is licensed under the MIT License - see the LICENSE file for details