Adding Days to a Date but Excluding Weekends

前端 未结 11 2162
萌比男神i
萌比男神i 2020-12-01 15:47

Given a date how can I add a number of days to it, but exclude weekends. For example, given 11/12/2008 (Wednesday) and adding five will result in 11/19/2008 (Wednesday) rath

相关标签:
11条回答
  • 2020-12-01 16:14

    using Fluent DateTime https://github.com/FluentDateTime/FluentDateTime

    var dateTime = DateTime.Now.AddBusinessDays(4);
    
    0 讨论(0)
  • 2020-12-01 16:18

    I would use this extension, remember since it is an extension method to put it in a static class.

    Usage:

    var dateTime = DateTime.Now.AddBusinessDays(5);
    

    Code:

    namespace ExtensionMethods
    {
        public static class MyExtensionMethods
        {
            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;
            }
        }
    }
    

    Source:

    https://github.com/FluentDateTime/FluentDateTime/blob/master/src/FluentDateTime/DateTime/DateTimeExtensions.cs

    0 讨论(0)
  • 2020-12-01 16:22
    int daysToAdd = weekDaysToAdd + ((weekDaysToAdd / 5) * 2) + (((origDate.DOW + (weekDaysToAdd % 5)) >= 5) ? 2 : 0);
    

    To wit; the number of "real" days to add is the number of weekdays you're specifying, plus the number of complete weeks that are in that total (hence the weekDaysToAdd / 5) times two (two days in the weekend); plus a potential offset of two days if the original day of the week plus the number of weekdays to add "within" the week (hence the weekDaysToAdd mod 5) is greater than or equal to 5 (i.e. is a weekend day).

    Note: this works assuming that 0 = Monday, 2 = Tuesday, ... 6 = Sunday. Also; this does not work on negative weekday intervals.

    0 讨论(0)
  • 2020-12-01 16:24
    public DateTime AddBusinessDays(DateTime dt, int nDays)
    {
        int weeks = nDays / 5;
        nDays %= 5;
        while(dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
            dt = dt.AddDays(1);
    
        while (nDays-- > 0)
        {
            dt = dt.AddDays(1);
            if (dt.DayOfWeek == DayOfWeek.Saturday)
                dt = dt.AddDays(2);
        }
        return dt.AddDays(weeks*7);
    }
    
    0 讨论(0)
  • 2020-12-01 16:27

    Here is how I did it.

    I had to calculate SLA (Service Level Agreement) due dates based on a start date and number of days, and account for weekends and public holidays:

        public DateTime? CalculateSLADueDate(DateTime slaStartDateUTC, double slaDays)
        {
            if (slaDays < 0)
            {
                return null;
            }
    
            var dayCount = slaDays;
            var dueDate = slaStartDateUTC;
    
            var blPublicHoliday = new PublicHoliday();
            IList<BusObj.PublicHoliday> publicHolidays = blPublicHoliday.SelectAll();
    
            do
            {
                dueDate = dueDate.AddDays(1);
    
                if ((dueDate.DayOfWeek != DayOfWeek.Saturday)
                && (dueDate.DayOfWeek != DayOfWeek.Sunday)
                && !publicHolidays.Any(x => x.HolidayDate == dueDate.Date))
                {
                    dayCount--;
                }
            }
            while (dayCount > 0);
    
            return dueDate;
        }
    

    blPublicHoliday.SelectAll() is a cached in-memory list of public holidays.

    (note: this is a cut down version for sharing publicly, there is a reason its not an extension method)

    0 讨论(0)
  • 2020-12-01 16:32

    Without over-complicating the algorithm, you could just create an extension method like this:

    public static DateTime AddWorkingDays(this DateTime date, int daysToAdd)
    {
        while (daysToAdd > 0)
        {
            date = date.AddDays(1);
    
            if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
            {
                daysToAdd -= 1;
            }
        }
    
        return date;
    }
    
    0 讨论(0)
提交回复
热议问题