Recipe 15.4 · C#
Business-Day Calculations
Problem
Due dates, SLAs, settlement dates and "respond within five business days" all need date arithmetic that skips weekends and holidays. The typical version loops with DateTime, ignores holidays or reads them from a table on every call, and has an off-by-one error at the edges that only shows up on a Friday.
The way it's often written
Anti-pattern: shown so you can recognize it. Don't copy this one.
// Not to copy: no holidays, unclear edges, DateTime with a time.
public static DateTime AddBusinessDays(DateTime date, int days)
{
while (days > 0)
{
date = date.AddDays(1);
if (date.DayOfWeek != DayOfWeek.Saturday &&
date.DayOfWeek != DayOfWeek.Sunday)
days--;
}
return date; // negative days? ignored
}
The improved recipe
using System;
using System.Collections.Generic;
namespace ImproveCode.Utilities;
public interface IHolidayCalendar
{
bool IsHoliday(DateOnly date);
}
public sealed class HolidaySet(IEnumerable<DateOnly> dates)
: IHolidayCalendar
{
private readonly HashSet<DateOnly> _dates = new(dates);
public bool IsHoliday(DateOnly date) => _dates.Contains(date);
}
public static class BusinessDays
{
public static bool IsBusinessDay(
DateOnly date, IHolidayCalendar holidays) =>
date.DayOfWeek is not (DayOfWeek.Saturday or DayOfWeek.Sunday)
&& !holidays.IsHoliday(date);
public static DateOnly Add(
DateOnly start, int businessDays, IHolidayCalendar holidays)
{
int step = Math.Sign(businessDays);
int remaining = Math.Abs(businessDays);
DateOnly date = start;
while (remaining > 0)
{
date = date.AddDays(step);
if (IsBusinessDay(date, holidays))
{
remaining--;
}
}
return date;
}
// Business days after start, up to and including end.
public static int Between(
DateOnly start, DateOnly end, IHolidayCalendar holidays)
{
if (end < start)
{
return -Between(end, start, holidays);
}
int count = 0;
for (DateOnly d = start.AddDays(1); d <= end; d = d.AddDays(1))
{
if (IsBusinessDay(d, holidays))
{
count++;
}
}
return count;
}
public static DateOnly NextOrSame(
DateOnly date, IHolidayCalendar holidays)
{
while (!IsBusinessDay(date, holidays))
{
date = date.AddDays(1);
}
return date;
}
}
If you need United States federal holidays, generate them instead of typing them in every January:
using System;
using System.Collections.Generic;
namespace ImproveCode.Utilities;
public static class UsFederalHolidays
{
public static IEnumerable<DateOnly> For(int year)
{
yield return Observed(new DateOnly(year, 1, 1));
yield return Nth(year, 1, DayOfWeek.Monday, 3);
yield return Nth(year, 2, DayOfWeek.Monday, 3);
yield return Last(year, 5, DayOfWeek.Monday);
if (year >= 2021)
{
yield return Observed(new DateOnly(year, 6, 19));
}
yield return Observed(new DateOnly(year, 7, 4));
yield return Nth(year, 9, DayOfWeek.Monday, 1);
yield return Nth(year, 10, DayOfWeek.Monday, 2);
yield return Observed(new DateOnly(year, 11, 11));
yield return Nth(year, 11, DayOfWeek.Thursday, 4);
yield return Observed(new DateOnly(year, 12, 25));
}
private static DateOnly Observed(DateOnly date) =>
date.DayOfWeek switch
{
DayOfWeek.Saturday => date.AddDays(-1),
DayOfWeek.Sunday => date.AddDays(1),
_ => date
};
private static DateOnly Nth(
int year, int month, DayOfWeek day, int n)
{
var first = new DateOnly(year, month, 1);
int offset = ((int)day - (int)first.DayOfWeek + 7) % 7;
return first.AddDays(offset + (7 * (n - 1)));
}
private static DateOnly Last(int year, int month, DayOfWeek day)
{
var last = new DateOnly(
year, month, DateTime.DaysInMonth(year, month));
int offset = ((int)last.DayOfWeek - (int)day + 7) % 7;
return last.AddDays(-offset);
}
}
Usage snippet:
var holidays = new HolidaySet(
UsFederalHolidays.For(2025)
.Concat(UsFederalHolidays.For(2026))
.Concat(UsFederalHolidays.For(2027)));
DateOnly due = BusinessDays.Add(
new DateOnly(2026, 7, 1), 5, holidays); // 2026-07-09
How it works
The calculation works on DateOnly, so a time of day can't push a date across midnight during conversion. IsBusinessDay is the single definition of a working day; change it once if your business works Saturdays.
Add steps one day at a time in either direction and counts only business days, so Add(date, -3, ...) goes back three working days. A loop sounds slow, but even a year of business days is a few hundred iterations, far below the cost of the database call that usually surrounds it.
Between uses the same convention as Add: it counts days after the start, up to and including the end. With that convention, if end is a business day, Add(start, Between(start, end)) returns end. Pick one convention, write it on the method, and test the edges. Most business-day bugs come down to a disagreement about whether the start day counts.
The holiday calendar is an interface so the source can vary: a HashSet built from code, a table loaded at startup, or a call to a calendar service. Hiding the lookup behind IsHoliday keeps the arithmetic free of I/O. In the example, July 3, 2026 is the observed Independence Day (July 4 falls on a Saturday), so five business days from July 1 lands on July 9.
UsFederalHolidays applies the federal observance rule: a holiday that falls on Saturday is observed on Friday, and one on Sunday on Monday. Note that when January 1 falls on a Saturday, the observed day is December 31 of the previous year, which is why the usage builds the set from three years.
Variations and pitfalls
- Your business holidays are rarely exactly the federal list. Banks follow the Federal Reserve calendar, which does not move Saturday holidays to Friday. Many companies add the day after Thanksgiving. Keep the calendar in a
dbo.Holidaytable that the business can maintain and load it into aHolidaySetat startup. - Different regions need different calendars. Make the calendar a parameter, never a static.
- For very large ranges, count whole weeks arithmetically and loop only over the remainder. Few business apps need it.
- In SQL, a calendar table with an
IsBusinessDaycolumn beats any function for reporting queries. Generate it from the same rules.