1

I am trying to implement a method in VB.NET that could subtract a start datetime and datetime, and result this specific string period "D.HH:mm:ss", observing that the day doesn't have 24 hours, but only 8 hours.

My function to do the subtract return only the diff in hours and as decimal:

Public Function WH(ByVal date1 As Date, ByVal date2 As Date, 
              Optional ByVal considerwk As Boolean = True) As Decimal

    Dim ini_tim As DateTime = DateTime.Parse("08:00")
    Dim end_tim As DateTime = DateTime.Parse("18:00")

    '//RESULT
    Dim _res As Integer

    '//WHILE INITIAL LESS THAN END...
    Do While date1 <= date2

        '//INSIDE INTERVAL?
        If Hour(date1) >= Hour(ini_tim) And Hour(date1) <= Hour(end_tim) Then

            '//CONSIDER WORKDAY?
            If considerwk = True Then
                '//IF NOT SATURDAY OR SUNDAY
                If Weekday(date1) <> vbSaturday And Weekday(date1) <> vbSunday Then

                    '//ADD +1 IN RESULT
                    _res += 1
                End If
            Else
                '//ADD +1 IN RESULT
                _res += 1
            End If
        End If

        '//ADD A MINUTE IN THE DATE
        date1 = DateAdd("n", 1, date1)
    Loop

    '//RETURN THE DIFF IN DEC
    Return CDec(_res / 60)

End Function

Hope that you can help me! Thanks!

AHiggins
  • 7,029
  • 6
  • 36
  • 54

1 Answers1

1

Something to consider...

With your current code:

Hour(date1) <= Hour(end_tim)

A time of 18:01 would be considered "inside the interval" when it's actually after the end time!

Try something more like below. It'll be accurate down to the second and can handle it if your start/stop times are not exactly at the top of the hour (like 08:30 to 18:30):

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt1 As DateTime = DateTime.Now
    Dim dt2 As DateTime = dt1.AddDays(5).AddHours(4).AddMinutes(39).AddSeconds(17)
    Dim ts As TimeSpan = WH(dt1, dt2, True)
    Dim strFormattedTotal As String = String.Format("{0}.{1}:{2}:{3}", ts.Days, ts.Hours.ToString("00"), ts.Minutes.ToString("00"), ts.Seconds.ToString("00"))
    Label1.Text = "Total: " & strFormattedTotal
    Label2.Text = "Total Hours: " & ts.TotalHours
End Sub

Public Function WH(ByVal date1 As Date, ByVal date2 As Date, Optional ByVal considerwk As Boolean = True) As TimeSpan
    Static ini_tim As DateTime = DateTime.Parse("08:00")
    Static end_tim As DateTime = DateTime.Parse("18:00")

    Dim TS As New TimeSpan
    Do While date1 < date2
        If date1.TimeOfDay >= ini_tim.TimeOfDay AndAlso date1.TimeOfDay <= end_tim.TimeOfDay Then
            If considerwk Then
                If date1.DayOfWeek <> DayOfWeek.Saturday AndAlso date1.DayOfWeek <> DayOfWeek.Sunday Then
                    TS = TS.Add(TimeSpan.FromSeconds(1))
                End If
            Else
                TS = TS.Add(TimeSpan.FromSeconds(1))
            End If
        End If
        date1 = date1.AddSeconds(1)
    Loop
    Return TS
End Function
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40