Calculate date add, but only on weekdays

I would like to calculate the new date simply using the built-in dateadd function, but keep in mind that you only need to consider weekdays (or "working days", so to speak).

I came up with this simple algorithm that does not worry about holidays, etc. I checked this with some simple dates, but would like some data if it can be done better.

This example assumes a week with 5 business days, Monday-Friday, where the first day of the week is Monday. The data format used here is dm-yyyy, the sample is calculated from the start date of October 1, 2009.

Here is a simple form:

Dim d_StartDate As DateTime = "1-10-2009"
Dim i_NumberOfDays As Integer = 12
Dim i_CalculateNumberOfDays As Integer 

If i_NumberOfDays > (5 - d_StartDate.DayOfWeek) Then
    i_CalculateNumberOfDays = i_NumberOfDays
Else
    i_CalculateNumberOfDays = i_NumberOfDays + (Int(((i_NumberOfDays + (7 - d_StartDate.DayOfWeek)) / 5)) * 2)
End If

MsgBox(DateAdd(DateInterval.Day, i_CalculateNumberOfDays, d_StartDate))

What I'm trying to explain with the following code snippet:

''' create variables to begin with
Dim d_StartDate as Date = "1-10-2009"
Dim i_NumberOfDays as Integer = 5

''' create var to store number of days to calculate with
Dim i_AddNumberOfDays as Integer 


''' check if amount of businessdays to add exceeds the 
''' amount of businessdays left in the week of the startdate
If i_NumberOfDays > (5 - d_StartDate.DayOfWeek) Then


    ''' start by substracting days in week with the current day,
    ''' to calculate the remainder of days left in the current week
    i_AddNumberOfDays = 7 - d_StartDate.DayOfWeek

    ''' add the remainder of days in this week to the total
    ''' number of days we have to add to the date
    i_AddNumberOfDays += i_NumberOfDays

    ''' divide by 5, because we need to know how many 
    ''' business weeks we are dealing with
    i_AddNumberOfDays = i_AddNumberOfDays / 5

    ''' multiply the integer of current business weeks by 2
    ''' those are the amount of days in the weekends we have 
    ''' to add to the total
    i_AddNumberOfDays = Int(i_AddNumberOfDays) * 2

    ''' add the number of days to the weekend days
    i_AddNumberOfDays += i_NumberOfDays

Else

    ''' there are enough businessdays left in this week
    ''' to add the given amount of days
    i_AddNumberOfDays = i_NumberOfDays

End If

''' this is the numberof dates to calculate with in DateAdd
dim d_CalculatedDate as Date
d_CalculatedDate = DateAdd(DateInterval.Day, i_AddNumberOfDays, d_StartDate)

Thanks in advance for your comments and comments.

+3
10

, . , , , , , / , , .

, , . , - , ( ). ( VB); . , isWeekend() isHoliday().

function addBusinessDays(startDate, numDays)
{
    Date newDate = startDate;
    while (numDays > 0)
    {
         newDate.addDays(1);
         if (newDate.isWeekend() || newDate.isHoliday())
             continue;

         numDays -= 1;
    }
    return newDate;
}

, , , , , . , - , .

0
Public Shared Function AddBusinessDays(ByVal startDate As DateTime, _
                                       ByVal businessDays As Integer) As DateTime
    Dim di As Integer
    Dim calendarDays As Integer

    '''di: shift to Friday. If it Sat or Sun don't shift'
    di = (businessDays - Math.Max(0, (5 - startDate.DayOfWeek)))

    ''' Start = Friday -> add di/5 weeks -> end = Friday'
    ''' -> if the the remaining (<5 days) is > 0: add it + 2 days (Sat+Sun)'
    ''' -> shift back to initial day'
    calendarDays = ((((di / 5) * 7) _
                   + IIf(((di Mod 5) <> 0), (2 + (di Mod 5)), 0)) _
                   + (5 - startDate.DayOfWeek))

    Return startDate.AddDays(CDbl(calendarDays))

End Function
+1

:

    dayOfWeek = startDate.DayOfWeek
    weekendDays = 2 * Math.Floor((dayOfWeek + businessDays - 0.1) / 5)
    newDate = startDate.AddDays(businessDays + weekendDays)

, , 2, .

-0.1 , , (dayOfWeek + businessDays) 5, - .

0

.DayOfWeek, , . . . , , . , , , . while.

Function AddBusinessDays(startDate As Date, numberOfDays As Integer) As Date
    Dim newDate As Date = startDate
    While numberOfDays > 0
        newDate = newDate.AddDays(1)

        If newDate.DayOfWeek() > 0 AndAlso newDate.DayOfWeek() < 6 Then '1-5 is Mon-Fri
            numberOfDays -= 1
        End If

    End While
    Return newDate
End Function
0
Private Function AddBusinessDays(ByVal dtStartDate As DateTime, ByVal intVal As Integer) As DateTime

    Dim dtTemp As DateTime = dtStartDate

    dtTemp = dtStartDate.AddDays(intVal)

    Select Case dtTemp.DayOfWeek
        Case 0, 6
            dtTemp = dtTemp.AddDays(2)
    End Select

    AddBusinessDays = dtTemp

End Function
0

,

Dim strDate As Date = DateTime.Now.Date
Dim afterAddDays As Date
Dim strResultDate As String
Dim n As Integer = 0

For i = 1 To 15
    afterAddDays = strDate.AddDays(i)
    If afterAddDays.DayOfWeek = DayOfWeek.Saturday Or afterAddDays.DayOfWeek = DayOfWeek.Sunday Then
        n = n + 1
    End If
Next

strResultDate = afterAddDays.AddDays(n).ToShortDateString()
0
Private Function AddXWorkingDays(noOfWorkingDaysToAdd As Integer) As Date

    AddXWorkingDays = DateAdd(DateInterval.Weekday, noOfWorkingDaysToAdd + 2, Date.Today)
    If Weekday(Today) + noOfWorkingDaysToAdd < 6 Then AddXWorkingDays = DateAdd(DateInterval.Weekday, 2, Date.Today)

End Function
0

, . iAddDays, dDate, - . , , , .

 Public Function DateAddWeekDaysOnly(ByVal dDate As DateTime, ByVal iAddDays As Int32) As DateTime
    If iAddDays <> 0 Then
        Dim iIncrement As Int32 = If(iAddDays > 0, 1, -1)
        Dim iCounter As Int32

        Do
            dDate = dDate.AddDays(iIncrement)
            If dDate.DayOfWeek <> DayOfWeek.Saturday AndAlso dDate.DayOfWeek <> DayOfWeek.Sunday Then iCounter += iIncrement
        Loop Until iCounter = iAddDays
    End If

    Return dDate
End Function
0

function addWerktage($date,$tage){
    for($t=0;$t<$tage;$t++){
        $date   = $date + (60*60*24);
        if(date("w",$date) == 0 || date("w",$date) == 6){ $t--; }
    }
    return $date;
}
0
Dim result As Date
    result = DateAdd("d", 2, Today)
    If result.DayOfWeek = DayOfWeek.Saturday Then
        result = DateAdd("d", 2, result)
        MsgBox(result)
    ElseIf result.DayOfWeek = DayOfWeek.Sunday Then
        result = DateAdd("d", 1, result)
        MsgBox(result)
    ElseIf result.DayOfWeek = DayOfWeek.Monday Then
        MsgBox(result)
    ElseIf result.DayOfWeek = DayOfWeek.Tuesday Then
        MsgBox(result)
    ElseIf result.DayOfWeek = DayOfWeek.Wednesday Then
        MsgBox(result)
    ElseIf result.DayOfWeek = DayOfWeek.Thursday Then
        MsgBox(result)
    ElseIf result.DayOfWeek = DayOfWeek.Friday Then
        MsgBox(result)
    End If
-2

Source: https://habr.com/ru/post/1720133/


All Articles