Count the number of weekdays between two dates in visual basic

后端 未结 6 1204
我寻月下人不归
我寻月下人不归 2021-01-14 21:48

I\'m a SQL guy, but I need a function to calculate the number of weekdays between two dates in VB.NET. I don\'t need to worry about holidays. My attempts unfortunately have

6条回答
  •  [愿得一人]
    2021-01-14 21:56

    I think this may help

    Public Shared Function WeekEndsBetweenDates(ByVal StartDate As Date, ByVal EndDate As Date) As Integer
        Dim wkday, wkend As Integer
        For i As Integer = 0 To DateDiff(DateInterval.Day, StartDate, EndDate)
    
            If DateAdd(DateInterval.Day, i, StartDate).DayOfWeek = DayOfWeek.Saturday Or DateAdd(DateInterval.Day, i, StartDate).DayOfWeek = DayOfWeek.Sunday Then
                wkend += 1
            Else
                wkday += 1
            End If
        Next
        Return wkend
    End Function
    

    Here, i returned "wkend" which is the weekend part of the variables in the function. Alternatively, you can change the returned value to "wkday" which is the number of weekdays between the selected dates. I hope this helps. It works for me though

提交回复
热议问题