Calculating elapsed time

G

Greg

I am trying to calculate difference between two different times and
everything is ok if times are from am to pm on a same day. How do I
calculate elapsed time between for example 02/14 10:20 pm and 02/16 9:30 pm?
I created 2 text boxes (day in and day out) where I select specific date from
a calendar. Then I have to more text boxes for time (time in and time out) I
also would like to display result in a text box in format for ex. The
difference is: 1 day 4 hours and 20 min. Thanks
 
K

Ken Sheridan

The following function will give you the elapsed time between two date/time
values either in the format h:nn:ss or, by means of the optional blnShowDays
atgument, d days hh:nn:ss

Public Function TimeDuration( _
dtmFrom As Date, _
dtmTo As Date, _
Optional blnShowDays As Boolean = False) As String

Const HOURSINDAY = 24
Dim lngHours As Long
Dim strMinutesSeconds As String
Dim strDaysHours As String
Dim dblDuration As Double

dblDuration = dtmTo - dtmFrom

'get number of days
lngHours = Int(dblDuration) * HOURSINDAY + _
Format(dblDuration, "h")

' get minutes and seconds
strMinutesSeconds = Format(dblDuration, ":nn:ss")

If blnShowDays Then
'get days and hours
strDaysHours = lngHours \ HOURSINDAY & _
" day" & IIf(lngHours \ HOURSINDAY <> 1, "s ", " ") & _
lngHours Mod HOURSINDAY

TimeDuration = strDaysHours & strMinutesSeconds
Else
TimeDuration = lngHours & strMinutesSeconds
End If

End Function

So:

TimeDuration(#02/17/2007 10:15:45#, #02/19/2007 18:33:50#)

returns 56:18:05

and:

TimeDuration(#02/17/2007 10:15:45#, #02/19/2007 18:33:50#, True)

returns 2 days 8:18:05

In your case just make the ControlSource of the third text box:

= TimeDuration([Day In]+[Time In], [Day Out]+[Time Out], True)

Ken Sheridan
Stafford, England
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top