Time Difference

A

amsuria

Hi..

how will I code this situation:

TIME = 13:30
no. of hrs = 1.5 (one and a half hours)

ANSWER: 12:00 hours

-OR-

TIME = 13:00
no. of hrs = 1.5 (one and a half hours)

ANSWER: 11:30 hours

----------------------
Im having a biiig problem solving this.....getting the difference of a time
against no. of hours...

thanks for help...

amsuria
 
M

missinglinq via AccessMonster.com

Where YourTime is the time you're starting with and Answer is the time 1.5
hours before YourTime.

Answer = Dateadd ("n",-90,YourTime)
 
S

SteveS

Since there is more than one way to skin a cat... <bg>

Amsuria,

I'm not sure what you want the result to be; decimal hours (11hrs 30 min) or
a time (11:30 AM). Or in 24 hour format with "hours" after it.

Here is a function that returns a time. If the time is 1:30 AM and you
subtract 5.5hours, it will return 8:00:00 PM. Per your example, the function
ignores dates.

'----beg code-------
Public Function TimeDiff(pdTime As Date, psInterval As Single)
'subtracts decinal hours from a time
'returns a time
'Usage: Timediff(#13:30#,1.5)
'or in query: TimeDiff(Mytime, HrsMins)

Dim decHours As Single

'convert time to dec hours since midnight
decHours = Hour(pdTime) + Minute(pdTime) / 60

'check if time is LT interval
If decHours < psInterval Then
'if yes, add 24 hours
decHours = decHours + 24
End If

decHours = decHours - psInterval

'return time
TimeDiff = TimeSerial(Int(decHours), (decHours - Int(decHours)) * 60, 0)

End Function
'----end code-------

You can use

Format(TimeDiff(#1:30#,5.5),"Short Time") & " hours"

to display the value like your examples, replacing the hard coded values
with control names (on a form) or field names (in a query).

HTH
 

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