[RESOLVED] [2005] TimeSpan TotalHours second shift incorrect
I have a timesheet app that takes in two sets of time in the following format hh:mm tt
example:
StartTime = 1:00 AM
EndTime = 1:15 PM
TotalHours = 12.25
I get these result because of the following code
Code:
CDate(strEndTime).Subtract(CDate(strStartTime)).TotalHours
The issue I have is that if the following scenario happens
example:
StartTime = 10:21 PM
EndTime = 2:21 AM
TotalHours = -20.00
Instead of it displaying 4 hours it display -20. It is possible that someone working the second shift would work from 10:21 PM to 2:21 AM.
How can I correct this issue?
Re: [2005] TimeSpan TotalHours second shift incorrect
If you don't specify that your end data is on the day after the start date then it is assumed that they are the same date, so the result of that calculation is exactly as it should be. Try this:
VB Code:
Dim startTime As Date 'Read is start time.
Dim endTime As Date 'Read in end time.
If endTime < startTime Then
endTime.AddDays(1)
End If
Dim duration As TimeSpan = endTime - startTime
Having said that, I would think that a properly written application of this type would take into account the date as well as the time, so there shouldn't be an issue.