Jumat, 21 Agustus 2020

Vb6: Datediff Menjumlah Hari Kerja


Pertanyaan dalam forum kadang kala memberi inspirasi buat nulis apa di blog. Kali ini menjawab pertanyaan wacana bagaimana mengitung hari kerja (weekday) dalam rentang waktu/tanggal tertentu tanpa menjumlah 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.



Sumber http://rani-irsan.blogspot.com


EmoticonEmoticon