.NET Date Compare Count the amount of working days since a date?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Counting working days in .NET usually means counting weekdays between two dates while excluding Saturday and Sunday, and sometimes also excluding holidays. The implementation is straightforward, but it helps to define the rules clearly first, especially whether the range is inclusive and whether time-of-day should be ignored.
Normalize to the Date Part First
If you are counting business days, the time portion usually does not matter. A common first step is to compare only the date component.
Using .Date avoids off-by-one confusion caused by partial days.
Count Weekdays with a Simple Loop
The clearest implementation is to walk day by day and count only Monday through Friday.
This version is inclusive of both start and end. If you want “since a date” to mean excluding the start day, move the loop to start.AddDays(1).
Excluding Holidays
Many business rules treat holidays as non-working days too. In that case, pass a set of holiday dates.
A HashSet<DateTime> is a good choice because holiday lookups stay efficient.
A Practical Usage Example
This counts only weekdays in the range and subtracts any holidays that land on those weekdays.
When Performance Matters
For typical date ranges, the simple loop is perfectly acceptable and easier to trust. If you are computing across very large spans or doing this thousands of times, you can derive a faster arithmetic solution using full weeks plus leftover days.
But unless performance is proven to be a problem, the loop version is often the best tradeoff between correctness and readability.
DateOnly Can Be Cleaner in Modern .NET
If you are on a modern .NET version, DateOnly models this concept more directly because business-day logic is fundamentally date-only logic.
You can still implement the same weekday loop, but the type itself prevents time-of-day confusion.
Common Pitfalls
A common mistake is forgetting to strip the time portion and then wondering why comparisons behave strangely.
Another pitfall is not defining whether the start date counts. “Since a date” can mean inclusive or exclusive depending on the business rule.
Developers also sometimes forget that holidays should be compared as dates only. If one holiday value contains a time component, Contains checks may fail unexpectedly.
Finally, do not over-engineer the first implementation. A clear inclusive loop is easier to validate than a clever formula you do not trust.
Summary
- Normalize to the date part before counting working days.
- Count Monday through Friday and exclude Saturday and Sunday.
- Add a holiday set if business rules require it.
- Be explicit about whether the range is inclusive or exclusive.
- Prefer a simple readable loop unless performance data proves you need a more complex formula.

