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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcriderCommented:
You can use the DateDiff function.

Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimCotteeHead of Software ServicesCommented:
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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
TimCotteeHead of Software ServicesCommented:
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
TimCotteeHead of Software ServicesCommented:
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
TimCotteeHead of Software ServicesCommented:
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
TimCotteeHead of Software ServicesCommented:
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
TimCotteeHead of Software ServicesCommented:
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
TimCotteeHead of Software ServicesCommented:
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
TimCotteeHead of Software ServicesCommented:
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
TimCotteeHead of Software ServicesCommented:
mscala

Wants computer seeing to!
0
mscalaAuthor Commented:
yes
it works
good stuff thanks a lot guys
0
TimCotteeHead of Software ServicesCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.