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!