VB.NET: DateAdd Menambah Hari Hanya dengan Hari Kerja


Rani pernah bahas gimana cara menghitung selisih waktu hari kerja, karena ada pertanyaan dari reader gimana klo menambahkan jumlah hari tapi hanya dengan hari kerja saja (weekdays). Langsung aja kita jawab dengan contoh ya.

Design UI:
Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Text = "From:"
        Label2.Text = "Add day(s):"
        Label3.Text = "To:"
        Button1.Text = "Add Weekday"
    End Sub


    Public Shared Function AddWeekdays(ByVal startDate As Date, _
                           ByVal interval As Integer) As Date
        Dim curDate As Date = startDate
        Dim i As Integer = 0

        Do While i < interval
            curDate = curDate.AddDays(1)
            If curDate.DayOfWeek <> DayOfWeek.Saturday And _
               curDate.DayOfWeek <> DayOfWeek.Sunday Then
                i = i + 1
            End If
        Loop


        Return curDate
    End Function
  

    Private Sub Button1_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles Button1.Click

        DateTimePicker2.Value = AddWeekdays(DateTimePicker1.Value, _
                                            CDbl(TextBox1.Text))

    End Sub
End Class


Runtime:



Click here if you like this article.


Post a Comment

8 Comments

Unknown said…
keren ini artikelnya.. boleh ya klo ada kondisi yg susah sya nanya sma admin..?
terimakasih sudah bantu
rani said…
silahkan, klo emang rani bisa jawab Insya Allah.
Unknown said…
This comment has been removed by the author.
Unknown said…
mbak mohon dibantu atau masukanya
sy punya kasus yg susah terpecahkan

sy sudah kirim email rincianya
terimakasih atas waktunya
rani said…
yang mana ya?
Unknown said…
This comment has been removed by the author.
Unknown said…
noreply-comment@blogger.com tu bukan email mbak
kalau bukan bolehh minta emailnya ?
rani said…
This comment has been removed by the author.