Posted By

chadj2 on 02/20/09


Tagged

day DateTime Business


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

flurk118
leonbda
umang_nine
madelk


Method for adding working days to a date


 / Published in: C#
 

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.

  1. public DateTime AddWorkingDays(DateTime dtFrom, int nDays)
  2. {
  3. // determine if we are increasing or decreasing the days
  4. int nDirection = 1;
  5. if (nDays < 0)
  6. {
  7. nDirection = -1;
  8. }
  9.  
  10. // move ahead the day of week
  11. int nWeekday = nDays % 5;
  12. while(nWeekday != 0)
  13. {
  14. dtFrom = dtFrom.AddDays(nDirection);
  15.  
  16. if (dtFrom.DayOfWeek != DayOfWeek.Saturday
  17. && dtFrom.DayOfWeek != DayOfWeek.Sunday)
  18. {
  19. nWeekday -= nDirection;
  20. }
  21. }
  22.  
  23. // move ahead the number of weeks
  24. int nDayweek = (nDays / 5) * 7;
  25. dtFrom = dtFrom.AddDays(nDayweek);
  26.  
  27. return dtFrom;
  28. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: MLaritz on March 4, 2010

You can simplify this by not using nWeekDay and just using nDays. I created an extension method:

public static class DateTimeExtensions
{
    public static DateTime AddBusinessDays(this DateTime date, int days)
    {
        int direction = days < 0 ? -1 : 1;

        while(days != 0)
        {
            date.AddDays(direction);
            if(date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
            {
                days -= direction;
            }
        }

        return date;
    }
}
Posted By: ybastiand on July 2, 2010

There is a bug in comment from MLaritz, at least when I change the method to be non extension. Fix is to assign the result of date.AddDays back in the date variable.

Here is my solution (unit tested): /// /// Add or remove some business days to a date time. /// /// /// /// public static DateTime AddBusinessDays(DateTime date, int days) { int direction = (days < 0) ? -1 : 1;

        while (days != 0)
        {
            date = date.AddDays(direction);
            if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
            {
                days -= direction;
            }
        }

        return date;
    }
Posted By: ybastiand on July 2, 2010

There is a bug in comment from MLaritz, at least when I change the method to be non extension. Fix is to assign the result of date.AddDays back in the date variable.

Here is my solution (unit tested): /// /// Add or remove some business days to a date time. /// /// /// /// public static DateTime AddBusinessDays(DateTime date, int days) { int direction = (days < 0) ? -1 : 1;

        while (days != 0)
        {
            date = date.AddDays(direction);
            if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
            {
                days -= direction;
            }
        }

        return date;
    }

You need to login to post a comment.