Hi and appreciate the help
I am trying to calculate the total hours worked when the shift end time passes to the next day. it seems everything is working fine except the tHours
is not right. The expected result should be 12 however it is coming out as 11
sShift = 08:00 PM (Start Shift)
eShift = 08:00 AM (End Shift) -> next day
Time1 = CDate(sShift.Text)
Time2 = CDate(eShift.Text)
If (Time2 <= Time1) Then
Total_Seconds = (Time2 1 - Time1) * 24 * 3600
Else
Total_Seconds = (Time2 - Time1) * 24 * 3600
End If
tMins = Int((Total_Seconds Mod 3600) / 60)
tHours = Int(Total_Seconds / 3600)
The Total_Seconds
is correct 43200, when divided over 3600 should result in 12. However, the tHours
is showing 11
CodePudding user response:
The issue is most likely caused by a floating point error. This article sheds some light on that.
My suggestion to fix your calculation in this case would be to use
If (time2 <= time1) Then
total_seconds = (time2 1 - time1) * 24 * 3600
Else
total_seconds = (time2 - time1) * 24 * 3600
End If
total_seconds = CLng(total_seconds)
And it is also good practise to use Option Explicit
.