Solution: Calculating Time Elapsed Between Two Dates

C

crferguson

Here are the components needed to calculate the days, hours, minutes,
and seconds between two dates...

Private Sub Difference()
Dim tStart As Date, tEnd As Date
Dim sTime As String, sDays As String
Dim sHours As String, sMin As String, sSec As String

tStart = Now - 0.12
tEnd = Now

sTime = DateDiff("s", tStart, tEnd)
sDays = ((((sTime / 60) / 60) / 24) Mod 60)
sHours = ((sTime / 60) / 60) Mod 24
sMin = (sTime / 60) Mod 60
sSec = sTime Mod 60
End Sub

tStart and tEnd are only in there to provide a couple of date values
to test the sub with. You can easily modify this to pass in a start
and end date like so:

Private Sub Difference(tStart As Date, tEnd As Date)

and then delete the three lines:

Dim tStart As Date, tEnd As Date
........
tStart = Now - 0.12
tEnd = Now

Thanks!
 
G

Guest

you have a problem here:

sDays = ((((sTime / 60) / 60) / 24) Mod 60)


Unless you want that to mean the number of days in the remainder after
dividing the number of days by 60; but not sure that is a standard of any
sort.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top