DayTwo
DayTwo

Reputation: 759

How do i subtract a number of business days (not Sat or Sunday) from a DateTime?

How do i subtract a number of business days (not Sat or Sunday) from a DateTime?

The method signature should be:

void SubtractBusinessDays(ref DateTime dt, int BusinessDaysToSubtract)

Thanks

Upvotes: 0

Views: 1555

Answers (2)

TimC
TimC

Reputation: 1061

Could you not just get the number of days, then mod 7, and subtract that value * 2 from the original number of days, then subtract 1 or 2 if the current day is saturday or sunday respectively?

Upvotes: 0

Brad
Brad

Reputation: 15577

Here's an extension method you can use. Feel free to refactor it as a simple method.

/// <summary>
/// Adds weekdays to date
/// </summary>
/// <param name="value">DateTime to add to</param>
/// <param name="weekdays">Number of weekdays to add</param>
/// <returns>DateTime</returns>
public static DateTime AddWeekdays(this DateTime value, int weekdays)
{
    int direction = Math.Sign(weekdays);
    int initialDayOfWeek = Convert.ToInt32(value.DayOfWeek);

    //---------------------------------------------------------------------------
    // if the day is a weekend, shift to the next weekday before calculating
    if ((value.DayOfWeek == DayOfWeek.Sunday && direction < 0)
        || (value.DayOfWeek == DayOfWeek.Saturday && direction > 0))
    {
        value = value.AddDays(direction * 2);
        weekdays += (direction * -1); // adjust days to add by one
    }
    else if ((value.DayOfWeek == DayOfWeek.Sunday && direction > 0)
        || (value.DayOfWeek == DayOfWeek.Saturday && direction < 0))
    {
        value = value.AddDays(direction);
        weekdays += (direction * -1); // adjust days to add by one
    }
    //---------------------------------------------------------------------------

    int weeksBase = Math.Abs(weekdays / 5);
    int addDays = Math.Abs(weekdays % 5);

    int totalDays = (weeksBase * 7) + addDays;
    DateTime result = value.AddDays(totalDays * direction);

    //---------------------------------------------------------------------------
    // if the result is a weekend, shift to the next weekday
    if ((result.DayOfWeek == DayOfWeek.Sunday && direction > 0)
        || (result.DayOfWeek == DayOfWeek.Saturday && direction < 0))
    {
        result = result.AddDays(direction);
    }
    else if ((result.DayOfWeek == DayOfWeek.Sunday && direction < 0)
        || (result.DayOfWeek == DayOfWeek.Saturday && direction > 0))
    {
        result = result.AddDays(direction * 2);
    }
    //---------------------------------------------------------------------------

    return result;
}

Upvotes: 2

Related Questions