Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating time. 1

Status
Not open for further replies.

ddmtn546

Technical User
Dec 20, 2000
38
US
Hello All! I need to calculate time difference in minutes on a form where the user enters: Date, Start Time, End Time. These times use military time.

In a query, I use:
1TimeDiff: DateDiff("s",[1StartTime],[1EndTime])/60

This works fine except when crossing midnight where going from, say, 2300 to 0000 or 0100 hrs the next day. I get negative values returned.

How would the code look to accomplish this?

Thanks in advance. ddmtn546.
 
iif(1starttime<=1endtime,DateDiff("s",[1StartTime],[1EndTime])/60, (DateDiff("s",[1StartTime],[1EndTime])+86400)/60)

 
Hi

You DateDiff() code is just fine, but if the start and finish time cross the midnight boundary, you need to include a start and finish 'time' which includes the date, if you do that Access will do the rest

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Lupins 46,
Thanks for the response, however I get a syntax error at the "1starttime" following the iif command, any ideas?

iif(1starttime<=1endtime,DateDiff("s",[1StartTime],[1EndTime])/60, (DateDiff("s",[1StartTime],[1EndTime])+86400)/60)
 
ddmtn546

Hey Lupins46, I', not sure how this works, but it does. I'll use it. Maybe you could email an explanation. Thanks,
ddmtn546@bellsouth.net
 
Hi

It is simply adding a day (in Seconds) to the answer, so it works for one span of 12 midnight, but not 2 or 3 etc

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top