Calculating elapsed time and adding time values

On Thu, 29 Jan 1998 11:14:44 -0500, John Glasscock

Quote:

>I want to have users record their TimeIn (e.g., 08:00 AM) and TimeOut

>(e.g., 05:00 PM), take the difference, subtract standard times for

>Breaks and Lunch, and return a value for the total elapsed time worked.

>Here is my code. Any help would be appreciated. Why can't Microsoft

It's easier than most people suppose: times are only doubles with the

date part in front of the decimal, and the time (as a fraction of a

day) to the right. If you want just the time of day from a date-time,

you can use MyDateVariable - Int(MyDateVariable) to get the fractional

part.

You can add them, average them, take the square root of them to your

heart's content. Remember to use Format(SomeTime, "hh:mm:ss") or

"ShortTime" or something for output.

Try this:

Private Sub Out_AfterUpdate()

Dim GrossTime As Double

Const LunchTime As Double = 1.0 / 24 / 2 ' = half an hour

Const BreakTime As Double = 1.0 / 24 / 6 ' = ten minutes

Const EightHours As Double = 1.0 / 24 * 8

Const SixHours As Double = 1.0 / 24 * 6

Const FourHours As Double = 1.0 / 24 * 4

GrossTime = [Out] - [In] ' This still works fine

' And the rest is

If GrossTime >= EightHours Then ' self-documenting...

[TotalTime] = GrossTime - BreakTime * 2 - LunchTime

ElseIf GrossTime >= SixHours Then

[TotalTime] = GrossTime - BreakTime - LunchTime

ElseIf GrossTime > FourHours Then

[TotalTime] = GrossTime - BreakTime

ElseIf GrossTime <= FourHours Then

[TotalTime] = GrossTime

End If

End Sub

All the best

Tim Ferguson