Calculate difference between times

K

Kim

I have two time fields and I need to calculate the time
lapse between the two fields. The result should be in
minutes. For example [Start time]=9:00 [End time]= 9:15
[difference]= 15

Thanks,
Kim
 
V

Van T. Dinh

TimeDiff: DateDiff("n", [StartTime], [EndTime])

should do. Check Access VB Help on the DateDiff() function.
 
K

Kim

Thanks for the response. This works fine, but I'm
getting negative numbers when my times change from AM to
PM. (ie. [StartTime] is 11:00PM, [EndTime] is 1:40 AM.
The result I'm getting is -1280 Instead of
160 What am I doing wrong?



Thanks again for your help.

Kim

-----Original Message-----
TimeDiff: DateDiff("n", [StartTime], [EndTime])

should do. Check Access VB Help on the DateDiff() function.

--
HTH
Van T. Dinh
MVP (Access)



Kim said:
I have two time fields and I need to calculate the time
lapse between the two fields. The result should be in
minutes. For example [Start time]=9:00 [End time]= 9:15
[difference]= 15

Thanks,
Kim


.
 
H

Howhiareu

You are getting those negative answers because the datediff function
believes that both times are in the same day if no date is supplied.
The number is negative, because the time used as the end time in
DateDiff is earlier than the start time used.

For it to work correctly, the 2 times that you are evaluating would
need to be complete dates like:

[starttime] = 1/13/2004 11:00 PM
[endtime] = 1/14/2004 1:30 AM

If you can, you should have the code that stores the time store it as a
long date instead, by using the Now() function if it is an automatic
entry. If not, then the code should supply the date when the record is
saved. The other alternative is to force some logic at the time you
calculate to handle it after the fact.

also if you add that negative number to 1440 (minutes/day), you get the
correct answer. (1440 + (-1280)) = 160 minutes
 

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