Drill

ProblemsC# › dates

Count the working days in a span

mediumdatesMathC#

An SLA clock only runs on working days, so a span of calendar days has to be reduced to the ones that count.

BusinessDays(startWeekday: int, spanDays: int) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int BusinessDays(int startWeekday, int spanDays) {
    
}

Worked examples

CallResult
BusinessDays(0, 7)5
BusinessDays(0, 5)5
BusinessDays(5, 2)0
BusinessDays(4, 4)2

Hint

Walk the days and take the weekday modulo 7, or work out the whole weeks first and handle the remainder.

Reference solution in C#
public int BusinessDays(int startWeekday, int spanDays) {
    if (spanDays <= 0 || startWeekday < 0 || startWeekday > 6) return 0;
    int n = 0;
    for (int i = 0; i < spanDays; i++) if ((startWeekday + i) % 7 < 5) n++;
    return n;
}

The same problem in another language

More dates problems in C#