Return to Snippet

Revision: 11831
at February 20, 2009 15:52 by chadj2


Initial Code
public DateTime AddWorkingDays(DateTime dtFrom, int nDays)
{
    // determine if we are increasing or decreasing the days
    int nDirection = 1;
    if (nDays < 0)
    {
        nDirection = -1;
    }

    // move ahead the day of week
    int nWeekday = nDays % 5;
    while(nWeekday != 0)
    {
        dtFrom = dtFrom.AddDays(nDirection);

        if (dtFrom.DayOfWeek != DayOfWeek.Saturday
            && dtFrom.DayOfWeek != DayOfWeek.Sunday)
        {
            nWeekday -= nDirection;
        }
    }

    // move ahead the number of weeks
    int nDayweek = (nDays / 5) * 7;
    dtFrom = dtFrom.AddDays(nDayweek);

    return dtFrom;
}

Initial URL


Initial Description
A quick search on the internet reveals all kinds of ways you can calculate with business days. I think is the most simple and efficient. In this example add day of week and weeks separately. The loop is never iterated more then  4 times regardless of the number of days being added.

Initial Title
Method for adding working days to a date

Initial Tags
Business

Initial Language
C#