VB6: DateDiff for Weekdays Only


Here is a sample for calculating number of days within specific date range for weekdays only (exclude saturday and sunday).

Design UI:
Code:
Option Explicit

Function CalculateWeekdays(
ByVal startDate As Date, ByVal endDate As Date) As Integer
    Dim numWeekdays As Integer
   
Dim totalDays As Integer
   
Dim WeekendDays As Integer
   
Dim i As Integer
   
    numWeekdays = 0
    WeekendDays = 0

    totalDays = DateDiff("d", startDate, endDate) + 1

    For i = 1 To totalDays

        If DatePart("w", startDate) = 1 Then
            WeekendDays = WeekendDays + 1
        End If
        If DatePart("w", startDate) = 7 Then
            WeekendDays = WeekendDays + 1
        End If
            startDate = DateAdd("d", 1, startDate)
    Next

    numWeekdays = totalDays - WeekendDays

    CalculateWeekdays = numWeekdays
End Function

Private Sub Command1_Click()
Label1.Caption = CalculateWeekdays(Me.DTPicker1.Value, Me.DTPicker2.Value)
End Sub


Runtime:



Click here if you like this article.


Post a Comment

0 Comments