C#
Working Days Calculation
Date Manipulation
Programming
Software Development

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.

csharp
1using System;
2
3public static class WorkingDayCalculator
4{
5    public static DateTime AddWorkingDays(DateTime startDate, int workingDays)
6    {
7        if (workingDays < 0)
8        {
9            throw new ArgumentOutOfRangeException(nameof(workingDays));
10        }
11
12        DateTime current = startDate;
13        int remaining = workingDays;
14
15        while (remaining > 0)
16        {
17            current = current.AddDays(1);
18
19            if (current.DayOfWeek != DayOfWeek.Saturday &&
20                current.DayOfWeek != DayOfWeek.Sunday)
21            {
22                remaining--;
23            }
24        }
25
26        return current;
27    }
28}

This is easy to read, easy to test, and correct for the common Monday-to-Friday case.

Example Usage

You can call it directly:

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        DateTime start = new DateTime(2026, 3, 6); // Friday
8        DateTime result = WorkingDayCalculator.AddWorkingDays(start, 3);
9
10        Console.WriteLine(result.ToString("yyyy-MM-dd"));
11    }
12}

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.

csharp
1using System;
2
3public static class WorkingDayMath
4{
5    public static DateTime AddWorkingDays(DateTime startDate, int workingDays)
6    {
7        DateTime current = startDate;
8        int direction = workingDays >= 0 ? 1 : -1;
9        int remaining = Math.Abs(workingDays);
10
11        while (remaining > 0)
12        {
13            current = current.AddDays(direction);
14
15            if (current.DayOfWeek != DayOfWeek.Saturday &&
16                current.DayOfWeek != DayOfWeek.Sunday)
17            {
18                remaining--;
19            }
20        }
21
22        return current;
23    }
24}

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.

csharp
1using System;
2using System.Collections.Generic;
3
4public static class BusinessCalendar
5{
6    public static DateTime AddWorkingDays(
7        DateTime startDate,
8        int workingDays,
9        HashSet<DateTime> holidays)
10    {
11        DateTime current = startDate;
12        int remaining = workingDays;
13
14        while (remaining > 0)
15        {
16            current = current.AddDays(1);
17
18            bool isWeekend =
19                current.DayOfWeek == DayOfWeek.Saturday ||
20                current.DayOfWeek == DayOfWeek.Sunday;
21
22            bool isHoliday = holidays.Contains(current.Date);
23
24            if (!isWeekend && !isHoliday)
25            {
26                remaining--;
27            }
28        }
29
30        return current;
31    }
32}

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.

Course illustration
Course illustration

All Rights Reserved.