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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time Difference

Status
Not open for further replies.

Hillary

Programmer
Feb 15, 2002
377
US
I want to evaluate the difference between two times. The Time In and Time Out formats are hh:mm:ss.

What is the best way to calculate the time difference?

Thanks,

Hillary
 
Check out the DateDiff function.

e.g.
duration = DateDiff("h",starttime,endtime)
 
I figured out the formula...I converted everything into seconds. How do I convert back to hh:mm:ss?

Thanks!

Hillary
 
There may be a better way but this should work assuming you don't ever have an end time after a start time.

Dim vTimeDuration as variant
Dim vHours As Variant
Dim vMinutes As Variant
Dim vSeconds As Variant

duration = DateDiff("s", starttime,endtime)

vHours = Format(Int(duration \ 3600), "00")
vMinutes = Format((duration Mod 3600) \ 60, "00")
vSeconds = Format((duration Mod 3600) Mod 60, "00")

vTimeDuration = vHours & ":" & vMinutes & ":" & vSeconds
 
Keep in mind that DateDiff works with the number of time-interval boundaries crossed between two times. It doesn't actually subtract the two.

That is,

[tt]DateDiff("m", starttime, endttime)[/tt]

will give different results than

[tt]DateDiff("m", 0, endtime-starttime)[/tt]

For the first way, the difference between 12:00:59 and 12:01:00 is 1 (a minute boundary has been crossed). The second way, the difference is 0 (less than a minute has elapsed).

You are using seconds, so the point may be moot here, but if you switch to a database such as SQL Server which stores datetime values with a resolution of 1/300th of a second you'd find your DateDiff function suddenly behaving differently.

Just like the Access example with minutes above, in SQL Server,

[tt]DateDiff(s, '12:00:00.997', '12:01:01.000')[/tt]

will give 1 second difference, even though only 3 milliseconds have passed.


 

and...

the UNITS of Time is DAYS, if you're considering converting Seconds to Time (Days).


Skip,

[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue]


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top