Hi
I'm trying to calculate the difference in working hours between a start and end date so I can calculate the labour cost for time spent on a job.
I can easily do the calculation if the visit is on the same day, but i'm struggling where the visit may span over a number of days.
Initially I tried having separate start and end date & time fields (txtDateStart, txtDateEnd, txtTimeStart and txtTimeEnd) and calculated the number of days x 8 hours then added in the hours, but I become stuck when the start date/time is 19/10/2011 16:00 and the end is 21/10/2011 10:00 because my time calculations where doing 10:00 - 16:00 which gave the wrong result.
So now I've created 1 field for each date/time with an inpur mask of 99/99/9999\ 00:00;0;_ and tried this code:
It seems to work but it knocks a few hours off the actual time... and this is where I'm getting a little stuck
With my start and end dates/times being 20/10/2011 08:00 - 22/10/2011 10:00 it shows the total hours worked as 9 but it should be 10
I really appreciate any help you can give
I'm trying to calculate the difference in working hours between a start and end date so I can calculate the labour cost for time spent on a job.
I can easily do the calculation if the visit is on the same day, but i'm struggling where the visit may span over a number of days.
Initially I tried having separate start and end date & time fields (txtDateStart, txtDateEnd, txtTimeStart and txtTimeEnd) and calculated the number of days x 8 hours then added in the hours, but I become stuck when the start date/time is 19/10/2011 16:00 and the end is 21/10/2011 10:00 because my time calculations where doing 10:00 - 16:00 which gave the wrong result.
So now I've created 1 field for each date/time with an inpur mask of 99/99/9999\ 00:00;0;_ and tried this code:
Code:
Dim datDate1 As Date
Dim datDate2 As Date
Dim dblNumDays As Double
Dim dblNumHours As Double
Dim dblWorkDaysInHours As Double
Dim dblHoursCheck As Double
Dim dblTotalWorkHours As Double
Dim dblDate1 As Double
Dim dblDate2 As Double
datDate1 = Me.Text2
datDate2 = Me.Text0
dblDate1 = CDbl(datDate1)
dblDate2 = CDbl(datDate2)
dblNumDays = dblDate2 - dblDate1
dblNumHours = dblNumDays * 24
Me.txtdiff = Abs(dblNumHours)
If dblNumDays > 0 Then
dblWorkDaysInHours = Round(dblNumDays * 8, 0) 'Based on 8 hours per day worked
dblHoursCheck = Round(dblNumDays * 16, 0)
dblTotalWorkHours = dblNumHours - dblHoursCheck
Me.txtdiff = Round(dblWorkDaysInHours + dblTotalWorkHours, 2)
Else
Me.txtdiff = Abs(dblNumHours)
End If
End Sub
It seems to work but it knocks a few hours off the actual time... and this is where I'm getting a little stuck
With my start and end dates/times being 20/10/2011 08:00 - 22/10/2011 10:00 it shows the total hours worked as 9 but it should be 10
I really appreciate any help you can give
