Calculating Time difference

A

Ayo

How do I calculate the difference betwenn a time range. I have defined my
startTime and endTime as follows, and I need to calculate the difference
between the two time period.

startTime = Format(Time, "hh:mm AM/PM")
endTime = Format(Time, "hh:mm AM/PM")

Me.txtTotalTime = Format(endTime - startTime, "hh:mm AM/PM")
 
J

Jeff Boyce

Ayo

You may be confusing the format of the displayed time with the way the data
is stored.

What you described seems to indicate that you have a field you've named
[Time] ... I believe Access treats that as a reserved word, so you might be
confusing yourself and Access with that.

Would it make any difference if something had a startTime just before
midnight and an endTime the next day?

More info, please...

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
R

Roger Carlson

Don't format your startTime and endTime. If your start and end times are
within the same 24 hour period, then you function to subtract them should
work.
 
K

Klatuu

Using the AM/PM would not be correct. Date/Time fields are a point in time.
The difference between two times is not a time, but a value. It would be the
number of hours, minutes, and seconds between the two.
 
K

Krzysztof Pozorek [MVP]

U¿ytkownik "Ayo said:
How do I calculate the difference betwenn a time range. I have defined my
startTime and endTime as follows, and I need to calculate the difference
between the two time period.

startTime = Format(Time, "hh:mm AM/PM")
endTime = Format(Time, "hh:mm AM/PM")

Me.txtTotalTime = Format(endTime - startTime, "hh:mm AM/PM")


Dim startTime As Date, endTime As Date
startTime = Now
'(...)
endTime = Now
Me.txtTotalTime = Format(endTime - startTime, "hh:mm AM/PM")

K.P.
www.access.vis.pl
 
K

Ken Sheridan

Create a function like this:

Function ElapsedTime(startTime As Date, endTime As Date) As String

Dim lngMinutes As Long

' get difference in minutes
lngMinutes = DateDiff("n", startTime, endTime)

' use integer division to get hours
'and Mod operator to get minutes
ElapsedTime = Format(lngMinutes \ 60, "00") & _
":" & Format(lngMinutes Mod 60, "00")

End Function

Call it by passing the start and ends of the range as values of date/time
data type. Unlike subtracting the values and formatting the result this will
cope with ranges of over 24 hours duration.

Ken Sheridan
Stafford, England
 
J

James A. Fortune

Ayo said:
How do I calculate the difference betwenn a time range. I have defined my
startTime and endTime as follows, and I need to calculate the difference
between the two time period.

startTime = Format(Time, "hh:mm AM/PM")
endTime = Format(Time, "hh:mm AM/PM")

Me.txtTotalTime = Format(endTime - startTime, "hh:mm AM/PM")

I posted some functions here:

http://groups.google.com/group/microsoft.public.access/browse_frm/thread/be7e101c5176ebd8

This was also interesting:

http://groups.google.com/group/microsoft.public.access/msg/a003152fa335dbe9

Maybe I should put them together :).

James A. Fortune
(e-mail address removed)
 

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