Adding Days to a Date but Excluding Weekends

前端 未结 11 2163
萌比男神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:32

    I created an extension that allows you to add or subtract business days. Use a negative number of businessDays to subtract. It seems to work in all cases.

    namespace Extensions.DateTime
    {
        public static class BusinessDays
        {
            public static System.DateTime AddBusinessDays(this System.DateTime source, int businessDays)
            {
                var dayOfWeek = businessDays < 0
                                    ? ((int)source.DayOfWeek - 12) % 7
                                    : ((int)source.DayOfWeek + 6) % 7;
    
                switch (dayOfWeek)
                {
                    case 6:
                        businessDays--;
                        break;
                    case -6:
                        businessDays++;
                        break;
                }
    
                return source.AddDays(businessDays + ((businessDays + dayOfWeek) / 5) * 2);
            }
        }
    }
    

    Example:

    using System;
    using System.Windows.Forms;
    using Extensions.DateTime;
    
    namespace AddBusinessDaysTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                label1.Text = DateTime.Now.AddBusinessDays(5).ToString();
                label2.Text = DateTime.Now.AddBusinessDays(-36).ToString();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 16:34

    This is better if anyone is looking for a TSQL solution. One line of code and works with negatives.

    CREATE FUNCTION[dbo].[AddBusinessDays](@Date date,@n INT)RETURNS DATE AS BEGIN 
    DECLARE @d INT;SET @d=4-SIGN(@n)*(4-DATEPART(DW,@Date));
    RETURN DATEADD(D,@n+((ABS(@n)+@d-2)/5)*2*SIGN(@n)-@d/7,@Date)END
    
    0 讨论(0)
  • 2020-12-01 16:35

    Given the number of the original day in the year D and original day in the week W and the number of workdays to add N, the next weekday number is

    W + N % 5.
    

    The next day in the year (with no wraparound check) is

    D + ((N / 5) * 7) + N % 5).
    

    This is assuming that you have integer division.

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

    Formula will be: Workday(date,no.of days,(weekday(1)))

    Try this. This will help.

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

    F# flavor of http://stackoverflow.com/questions/1044688 's answer:

    namespace FSharpBasics
    
    module BusinessDays =
    
        open System;
    
        let private weekLength = 5
    
        (*operation*)
        let addBusinessDays (numberOfBusinessDays: int) (startDate: DateTime) =
            let startWeekDay = startDate.DayOfWeek
            let sign = Math.Sign(numberOfBusinessDays) 
            let weekendSlide, businessDaysSlide = 
                match startWeekDay with
                | DayOfWeek.Saturday when sign > 0 -> (2, -1)
                | DayOfWeek.Saturday when sign < 0 -> (-1, 1)   
                | DayOfWeek.Sunday when sign > 0 -> (1, -1)
                | DayOfWeek.Sunday when sign < 0 -> (-2, 1)
                | _ -> (0, 0)
            let baseStartDate = startDate.AddDays (float weekendSlide)        
            let days = Math.Abs (numberOfBusinessDays + businessDaysSlide) % weekLength
            let weeks = Math.Abs (numberOfBusinessDays + businessDaysSlide) / weekLength
            let baseWeekDay = int baseStartDate.DayOfWeek
            let oneMoreWeekend =
                if sign = 1 && days + baseWeekDay > 5 || sign = -1 && days >= baseWeekDay then 2
                else 0
            let totalDays = (weeks * 7) + days + oneMoreWeekend
            baseStartDate.AddDays (float totalDays)
    
        [<EntryPoint>]
        let main argv =
            let now = DateTime.Now 
            printfn "Now is %A" now
            printfn "13 business days from now would be %A" (addBusinessDays 13 now)
            System.Console.ReadLine() |> ignore
            0 
    
    0 讨论(0)
提交回复
热议问题