# 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..
Thanks
###### 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.

Commented:
You can use the DateDiff function.

Cheers!
0

Experts Exchange Solution brought to you by

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

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
Author Commented:
TimCotee

Do you have to declare the:
diff, hrs, mins, secs, "s"
0
Commented:
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
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
Thanks mcrider for pointing out my obvious mistake, comes from bashing the keyboard without thinking too much.
0
Commented:
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
Commented:
TimCottee,

The line:

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

returns a long type not an integer type...

Cheers!

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

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

Cheers!
0
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
Author Commented:
mcrider

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

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

Does the TimeDiff have to be declared somewhere?
It says sub or function is not defined?
0
Commented:
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
Author Commented:
mcrider

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

Are you stuck in a loop? ;-)

0
Author Commented:
mcrider

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

yeah i am?
0
Commented:
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
Author Commented:
mcrider

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

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

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

in that code now,
what must declare diff, hrs, mins, secs as?
0
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
Author Commented:
The declarations?

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

Wants diff, and hrs etc, difined?
0
Commented:
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
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
Author Commented:
The declarations?

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

Wants diff, and hrs etc, difined?
0
mscala

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

Cheers!
0
Author 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.