C Adding working days from a cetain date
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Adding working days is different from adding calendar days because weekends, and sometimes holidays, must be skipped. In C#, the safest starting point is a small loop that moves day by day and only counts Monday through Friday, then extend it later if your business rules include holidays or custom weekends.
Start with a Clear Rule
Before writing code, define what "working day" means in your system. The most common rule is:
- Monday through Friday count
- Saturday and Sunday do not count
- holidays may or may not count, depending on business rules
If you do not define that rule first, date logic becomes ambiguous quickly.
Basic Implementation in C#
This method adds a positive number of working days and skips weekends.
This is easy to read, easy to test, and correct for the common Monday-to-Friday case.
Example Usage
You can call it directly:
Starting from Friday and adding three working days gives the following Wednesday, because Saturday and Sunday are skipped.
Supporting Negative Values
If your application also needs to subtract working days, handle positive and negative movement explicitly rather than forcing callers to invent separate functions.
This keeps the API more practical for real scheduling code.
Adding Holiday Support
Weekend-only logic is often not enough. A real business calendar usually excludes fixed holidays too. The simplest extension is to accept a set of blocked dates.
This design keeps the weekday rule separate from holiday data, which is easier to test and maintain.
Time-of-Day Considerations
DateTime.AddDays preserves the time component. If you start at 2026-03-06 15:30, the result will usually keep 15:30. That is often correct, but not always.
If the logic is purely date-based, normalize with .Date before processing or use DateOnly in newer .NET code when time-of-day should not exist at all.
Should You Optimize the Loop
For small ranges, the loop is completely fine. If you are adding thousands of working days in a hot path, you can optimize by jumping whole weeks and then handling the remainder. Do not start there unless you have measured a real need. The simple loop is easier to trust and review.
Common Pitfalls
The most common mistake is forgetting to define whether the start date itself counts as a working day. Another is assuming that skipping weekends is enough when the business also has holidays. Teams also often ignore the time portion of DateTime and later get surprising timestamps. Finally, premature attempts to micro-optimize the algorithm can introduce off-by-one bugs in cases that a simple day-by-day loop handles correctly.
Summary
- "Working day" needs a clear business definition before you write the code.
- A simple loop that skips Saturday and Sunday is the safest baseline solution.
- Add holiday support by checking against a set of excluded dates.
- Be explicit about whether negative offsets and time-of-day should be supported.
- Optimize only if profiling shows the simple implementation is not enough.

