VB6: DateDiff Menghitung Hari Kerja


Pertanyaan dalam forum seringkali memberi ide buat nulis apa di blog. Kali ini menjawab pertanyaan tentang bagaimana mengitung hari kerja (weekday) dalam rentang waktu/tanggal tertentu tanpa menghitung hari sabtu dan minggu.
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