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

Status
Not open for further replies.

JMTS

Programmer
Oct 31, 2000
26
US
I am have a brain cramp. I have two variables, one with a start time, another with an end time. I need to calculate the difference between the two in hours, minutes and seconds. So, for example, my start time is 06:55:32pm, my end time is 07:07:07pm, so the total time accumulated is 11 minutes 35 seconds. Is there a function in VB to calculate this?? Any help would be great.

Jason
 
yes, the DateDiff function will solve your problem here:

Dim nHours as Integer
Dim nMinutes as Integer
Dim nSeconds as Integer

nHours = DateDiff("h",starttime,endtime)
nMinutes = DateDiff("n",starttime,endtime) - nHours * 60
nSeconds = DateDiff("s",starttime,endtime) - nHours * 3600 - nMinutes * 60

Hope that helps.
Good Luck!

Regards,
Michael Bronner
 
Michael

Your code does not work!!

If you place a start time of 07:45:00 and an endtime of 08:00:00 you would expect 00:15:00, instead you get:

nHours = 1
nMinutes = 75
nSeconds = 1800

This would appear slightly incorrect!!


 
Hmmmmmmmmmmm,

Of course it doesn't. DateDiff "calculaes" the INTERVALS between the variables, not the actual elapsed time difference.

You DO NOT need to "do the math".

? Format((TimeEnd - TimSt), "hh:mm:ss")
00:11:35
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Right up to the moment you cross a 24 hour boundary, Michael...
 
If you use seconds as the base for the calculations, then you will not have to worry about the interval - unless you are interested in partial seconds. You can change the start/end times to a date if desired, and you can pass a Date/Time or just a Time. (This has ALSO been tested in non-US format).

Sub CalcTimePassed(ByVal StartTime As String, ByVal EndTime As String)

Dim lSecondsRemaining As Long
Dim lHours As Long
Dim lMinutes As Long

If Not (IsDate(StartTime) And IsDate(EndTime)) Then
'Code here to Err.Raise, Goto ErrHandler, or MsgBox
MsgBox "Must supply valid Time"
Exit Sub
End IF

lSecondsRemaining = DateDiff("s", StartTime, EndTime)

lHours = Fix(lSecondsRemaining / 3600)

lSecondsRemaining = lSecondsRemaining - (lHours * 3600)

lMinutes = lSecondsRemaining / 60

lSecondsRemaining = lSecondsRemaining - (lMinutes * 60)

Debug.Print "Hours: " & lHours
Debug.Print "Minutes: " & lMinutes
Debug.Print "Seconds: " & lSecondsRemaining

End Sub
 
strongm,

alas tis true. On the other hand, it is really quite simple to add the "days" simply by taking the "Int" (sorry CInt) of the accumulation and the 'short time' shown above.

(it WAS a 'quickie' - based on the examples of just a few minutes.)

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top