How to calculate business days in C#
There is no BCL business-day type; here is the honest landscape.
1. Roll your own over DateOnly
static DateOnly AddBusinessDays(DateOnly d, int n, HashSet<DateOnly> holidays)
{
int step = Math.Sign(n), left = Math.Abs(n);
while (left > 0)
{
d = d.AddDays(step);
if (d.DayOfWeek is not (DayOfWeek.Saturday or DayOfWeek.Sunday)
&& !holidays.Contains(d)) left--;
}
return d;
}
The code is the easy 10%. The holidays set, per country, with substitute
days, updated when governments change rules, is the hard 90%.
2. One HTTP call
var res = await http.GetFromJsonAsync<JsonElement>(
"https://<host>/v1/add?region=GB&start=2027-12-23&days=3");
// skips Christmas, Boxing Day AND both substitute days -> 2027-12-30Need this in code? The API answers the same questions over HTTPS with one GET request. Free tier of 100 calls a day, and the playground needs no key at all.