Count the number of weekdays between two dates in visual basic

后端 未结 6 1115
我寻月下人不归
我寻月下人不归 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条回答
  •  -上瘾入骨i
    2021-01-14 22:17

    'This is my version vb.net 2013 and its works

    Private Sub enddate_ValueChanged(sender As Object, e As EventArgs) Handles enddate.ValueChanged
            Dim countsun As Integer = 0
            Dim countsat As Integer = 0
            Dim nonholiday As Integer = 0
            Dim totalDays = (enddate.Value - startdate.Value).Days
            For i = 0 To totalDays
                Dim Weekday As DayOfWeek = startdate.Value.Date.AddDays(i).DayOfWeek
                If Weekday = DayOfWeek.Saturday Then
                    countsat += 1
                End If
                If Weekday = DayOfWeek.Sunday Then
                    countsun += 1
                End If
                If Weekday <> DayOfWeek.Saturday AndAlso Weekday <> DayOfWeek.Sunday Then
                    nonholiday += 1
                End If
            Next
            lblSumHeadCount.Text = nonholiday & " Day(s)"
        End Sub
    

    'Thanks Gabe

提交回复
热议问题