-1

How do you subtract Business days in C# based on this for Adding Business days:

    public static DateTime AddBusinessDays(DateTime date, int days = 5)
    {
        if (days < 0)
        {
            throw new ArgumentException("days cannot be negative", "days");
        }

        if (days == 0) return date;

        if (date.DayOfWeek == DayOfWeek.Saturday)
        {
            date = date.AddDays(2);
            days -= 1;
        }
        else if (date.DayOfWeek == DayOfWeek.Sunday)
        {
            date = date.AddDays(1);
            days -= 1;
        }

        date = date.AddDays(days / 5 * 7);
        int extraDays = days % 5;

        if ((int)date.DayOfWeek + extraDays > 5)
        {
            extraDays += 2;
        }

        return date.AddDays(extraDays);

    }

That cannot take negative numbers, so need another one specifically for subtracting business days.

Edit: That "duplicate" question is measuring the difference between two dates with business days. I just want a starting date, and amount of days to subtract to come up with the result. This to be done as a function, not as a extension, as you see above for AddDays.

And a method without loops as you see above would be the most efficient.

Stem Step
  • 61
  • 1
  • 12
  • 3
    Possible duplicate of [Calculate the number of business days between two dates?](http://stackoverflow.com/questions/1617049/calculate-the-number-of-business-days-between-two-dates) – Bob G Sep 26 '16 at 18:37
  • @DarkBobG That's not what I'm looking for though. – Stem Step Sep 26 '16 at 18:55

1 Answers1

1

See this Answer

(Linked answer has been duplicated below for convenience)

using Fluent DateTime:

var now = DateTime.Now;
var dateTime1 = now.AddBusinessDays(3);
var dateTime2 = now.SubtractBusinessDays(5);

internal code is as follows

/// <summary>
/// Adds the given number of business days to the <see cref="DateTime"/>.
/// </summary>
/// <param name="current">The date to be changed.</param>
/// <param name="days">Number of business days to be added.</param>
/// <returns>A <see cref="DateTime"/> increased by a given number of business days.</returns>
public static DateTime AddBusinessDays(this DateTime current, int days)
{
    var sign = Math.Sign(days);
    var unsignedDays = Math.Abs(days);
    for (var i = 0; i < unsignedDays; i++)
    {
        do
        {
            current = current.AddDays(sign);
        }
        while (current.DayOfWeek == DayOfWeek.Saturday ||
            current.DayOfWeek == DayOfWeek.Sunday);
    }
    return current;
}

/// <summary>
/// Subtracts the given number of business days to the <see cref="DateTime"/>.
/// </summary>
/// <param name="current">The date to be changed.</param>
/// <param name="days">Number of business days to be subtracted.</param>
/// <returns>A <see cref="DateTime"/> increased by a given number of business days.</returns>
public static DateTime SubtractBusinessDays(this DateTime current, int days)
{
    return AddBusinessDays(current, -days);
}
Community
  • 1
  • 1
P. Roe
  • 2,077
  • 1
  • 16
  • 23