RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: .NET
Expertise: Advanced
Sep 11, 2009



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Adding Workdays to a Date

This example adds the days of the week and the weeks separately. You never iterate over the loop more then four times regardless of the number of days being added.

After adding the day of the week, simply add seven days for every five business days to calculate the final result. Note that the function works for both future and past dates:
public DateTime AddWorkingDays(DateTime dtFrom, int nDays)
    int nDirection = 1;
    if (nDays < 0) { nDirection = -1; }
    int nWeekday = nDays % 5;
    while(nWeekday != 0)
        dtFrom = dtFrom.AddDays(nDirection);
        if (dtFrom.DayOfWeek != DayOfWeek.Saturday
            && dtFrom.DayOfWeek != DayOfWeek.Sunday)
            nWeekday -= nDirection;
    int nDayweek = (nDays / 5) * 7;
    dtFrom = dtFrom.AddDays(nDayweek);
    return dtFrom;
Chad Juliano
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date