Calculate business days in C#
The base class library has no business-day type. Implement the
loop over DateOnly, or call an API that carries the calendars.
Option 1: implement it
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;
}
You supply the holidays set per country and keep it current,
including substitute days (when 2027-12-25 falls on a Saturday, the UK observes
2027-12-27 and 2027-12-28).
Option 2: Dierum API
using var http = new HttpClient();
http.DefaultRequestHeaders.Add("X-Api-Key", key);
var json = await http.GetFromJsonAsync<JsonElement>(
"https://dierum.io/v1/add?region=GB&start=2027-12-23&days=3");
json.GetProperty("result").GetString(); // "2027-12-30"
The GB calendar includes the substitute days above, so the result lands on
the 30th without local holiday data. Reference covers
/v1/count, /v1/due-date, and batch calls for invoice
runs.
Need 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.