Subtracting Times

i would like to work out the time difference between two times. Eg: if someone enters a building at 08:20:35am and leaves at 10:15:42am, i would like to work out exactly how long he has been there, well, preferably, the hours and minutes he has been there..
Can anyone help please?
Thanks
mscalaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mcriderConnect With a Mentor Commented:
You can use the DateDiff function.

Cheers!
0
 
TimCotteeCommented:
Private Sub TimeDiff(StartTime, EndTime)
    diff = Abs(DateDiff("s", StartTime, EndTime))
    hrs = Int(diff / 3600)
    diff = diff - hrs * 3600
    mins = Int(diff / 60)
    diff = diff - mins * 60
    secs = diff
    TimeDiff = Format(hrs, "00:") & Format(mins, "00:") & Format(secs, "00")
End Sub

This function returns the difference between two times using the datadiff function mcrider mentioned.
0
 
mscalaAuthor Commented:
TimCotee

Do you have to declare the:
diff, hrs, mins, secs, "s"
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mcriderCommented:
TimCotee...

Mixing sub and function...

That Code should actually be:

Function TimeDiff(StartTime, EndTime)
    diff = Abs(DateDiff("s", StartTime, EndTime))
    hrs = Int(diff / 3600)
    diff = diff - hrs * 3600
    mins = Int(diff / 60)
    diff = diff - mins * 60
    secs = diff
    TimeDiff = Format(hrs, "00:") & Format(mins, "00:") & Format(secs, "00")
End Function


and you don't have to define anything, just drop it into a module

Cheers!
0
 
TimCotteeCommented:
It would be good practice to, they are all integers except for the "s" which is a parameter to the datediff function and therefore doesn't need to be declared. Looking at this now I notice that I have put the starttime and endtime the wrong way round in the datediff function but the ABS around it will sort that out anyway.
0
 
TimCotteeCommented:
Thanks mcrider for pointing out my obvious mistake, comes from bashing the keyboard without thinking too much.
0
 
mcriderCommented:
By the way, you would use it this way:

   ElapsedTime=TimeDiff("10/29/99 7:34:02","10/29/99 17:02:34")

Cheers!
0
 
mcriderCommented:
TimCottee,

The line:

diff = Abs(DateDiff("s", StartTime, EndTime))

returns a long type not an integer type...

Cheers!

0
 
TimCotteeCommented:
Again, you pick me up (correctly I might add) I didn't check before I replied!
0
 
mscalaAuthor Commented:
mcrider

Does the TimeDiff have to be declared somewhere?
It says sub or function is not defined?
0
 
mcriderCommented:
No, it is a native VB function, just use it....


Cheers!
0
 
TimCotteeCommented:
As mcrider pointed out, the code I gave you should read

Private Function TimeDiff(......
.....

End Function

This should be pasted into a module or a form. If it is in a module you may need to make it Public rather than private!
0
 
mscalaAuthor Commented:
mcrider

Does the TimeDiff have to be declared somewhere?
It says sub or function is not defined?
0
 
mscalaAuthor Commented:
mcrider

Does the TimeDiff have to be declared somewhere?
It says sub or function is not defined?
0
 
TimCotteeCommented:
mscala, you appear to have posted the same comment three times now, have you seen my last comment to this?
0
 
mscalaAuthor Commented:
mcrider

Does the TimeDiff have to be declared somewhere?
It says sub or function is not defined?
0
 
mcriderCommented:
Ok, do it like this:

1) create a module.

2) paste the following code into the Declarations Section of the module:


Public Function TimeDiff(StartTime, EndTime)
    diff = Abs(DateDiff("s", StartTime, EndTime))
    hrs = Int(diff / 3600)
    diff = diff - hrs * 3600
    mins = Int(diff / 60)
    diff = diff - mins * 60
    secs = diff
    TimeDiff = Format(hrs, "00:") & Format(mins, "00:") & Format(secs, "00")
End Function

3) Then you can call TimeDiff anywhere in your program.


Cheers!



0
 
mscalaAuthor Commented:
mcrider

Does the TimeDiff have to be declared somewhere?
It says sub or function is not defined?
0
 
mcriderCommented:
mscala,

Are you stuck in a loop? ;-)

0
 
mscalaAuthor Commented:
mcrider

Does the TimeDiff have to be declared somewhere?
It says sub or function is not defined?
0
 
mscalaAuthor Commented:
mcrider

yeah i am?
0
 
mcriderCommented:
Are you old enough to remember LP Records.... and the trick of putting a penny on the stylus to keep it from skipping and repeating the same set of grooves?  (BIG SMILE!)
0
 
mscalaAuthor Commented:
mcrider

yeah i am?
0
 
mscalaAuthor Commented:
Nah i don't know that stuff

in that code now,
what must declare diff, hrs, mins, secs as?
0
 
mcriderCommented:
consider yourself pennied (BIGGEST SMILE I GOT!)

Did you get the step-by-step I gave you to work?
0
 
mscalaAuthor Commented:
Nah i don't know that stuff

in that code now,
what must declare diff, hrs, mins, secs as?
0
 
TimCotteeCommented:
diff is a long, the others are integer

diff is a long, the others are integer

diff is a long, the others are integer

diff is a long, the others are integer

diff is a long, the others are integer

0
 
mscalaAuthor Commented:
The declarations?

Wants diff, and hrs etc, difined?
0
 
mscalaAuthor Commented:
The declarations?

Wants diff, and hrs etc, difined?
0
 
mcriderCommented:
Public Function TimeDiff(StartTime, EndTime)

    Dim diff as long
    dim hrs as integer
    dim mins as integer
    dim secs as integer

    diff = Abs(DateDiff("s", StartTime, EndTime))
    hrs = Int(diff / 3600)
    diff = diff - hrs * 3600
    mins = Int(diff / 60)
    diff = diff - mins * 60
    secs = diff
    TimeDiff = Format(hrs, "00:") & Format(mins, "00:") & Format(secs, "00")
End Function

0
 
TimCotteeCommented:
Putting my penny on again!

Dim diff as Long
Dim hrs as Integer
Dim mins as Integer
Dim secs as Integer

Though I think the needle is going to break soon!
0
 
mscalaAuthor Commented:
The declarations?

Wants diff, and hrs etc, difined?
0
 
mscalaAuthor Commented:
The declarations?

Wants diff, and hrs etc, difined?
0
 
TimCotteeCommented:
mscala

Wants computer seeing to!
0
 
mscalaAuthor Commented:
yes
it works
good stuff thanks a lot guys
0
 
TimCotteeCommented:
At last an end! Glad to be of help to you.
0
 
mcriderCommented:
Tim, I've posted a 50 point question for you... All you have you do is answer it and they're yours!

Cheers!
0
 
mscalaAuthor Commented:
yes
it works
good stuff thanks a lot guys
0
All Courses

From novice to tech pro — start learning today.