# Time Elapsed Between Dates.

What is the easiest way to get the time elapsed between two dates like this?

[code]Dim dtStartTime As New Date
Dim dtNow As New Date
Dim strTimeElapsed As String

// dtNow - dtStartTime = str(strTimeElapsed) ???

msgBox strTimeElapsed[/code]

I need it to be in hh:mm:ss format.

Thanks!

date2 - date1 will give you the elapsed seconds. From there, it’s easy enough to write code that will break it down and format it for you.

There is no native method that does this, if that’s what you’re asking.

Have a look at Xojo.Core.DateInterval http://developer.xojo.com/xojo-core-dateinterval

Thanks for the replies I was hoping there was an easier way.

Off the top of my head:

``````Function HMS(secs As Integer) As String
dim days as integer = secs \\ ( 60 * 60 * 24 )
secs = secs - ( days * ( 60 * 60 * 24 ) )

dim hours as integer = secs \\ ( 60 * 60 )
secs = secs - ( hours * ( 60 * 60 ) )

dim mins as integer = secs \\ 60
secs = secs - ( mins * 60 )

dim parts() as string
if days <> 0 then
parts.Append str( days )
end if

if hours <> 0 then
parts.Append format( hours, "00" )
end if

parts.Append format( mins, "00" )
parts.Append format( secs, "00" )

return join( parts, ":" )

End Function``````

Wow thanks for the example.

```Function SecondsToHHMMSS(d As Double) As String Dim h As Integer = Floor(d / 3600) Dim m As Integer = Floor(d Mod 3600 / 60) Dim s As Integer = Floor(d Mod 3600 Mod 60) Return Format(h, "00") + ":" + Format(m, "00") + ":" + Format(s, "00") End Function```
Use it like this:

```Dim dtStartTime As New Date() Dim dtNow As New Date() MsgBox SecondsToHHMMSS(dtNow.TotalSeconds - dtStart.TotalSeconds)```

``` Dim dtStartTime As New Date Dim dtNow As New Date Dim i As Integer = dtNow.TotalSeconds - dtStartTime.TotalSeconds Dim dtresult As New Date dtresult.TotalSeconds = i MsgBox dtresult.sqldatetime.NthField(" ",2) ```

Did you see our DateDifferenceMBS class?
Does more and gives you a lot of details on difference.

http://www.great-white-software.com/REALbasic_Code.html
look for delta
posted years ago

http://great-white-software.com/gws-rb-samples/deltaTime.zip

Clever!

@Wayne Golding Thanks! Exactly what I needed!

Just keep in mind that Wayne’s solution, while elegant, will not work when the difference is greater than 23:59 hours.

Thank you Kem, I really appreciate your help.

Wayne’s solution modified to give larger intervals :

``` Dim dtStartTime As New Date(2014, 12, 24, 00, 00, 00) // YYY, MM, DD, HH, MN, SS Dim dtNow As New Date Dim i As double = dtNow.TotalSeconds - dtStartTime.TotalSeconds Dim dtresult As New Date dtresult.TotalSeconds = i MsgBox str(max(0,dtresult.year-1905))+" Years, "+str(dtresult.month)+" Months, "+str(dtresult.Day)+" Days, " _ +str(dtresult.Hour)+" Hours, " +str(dtresult.Minute)+" Minutes, " +str(dtresult.Second)+" Seconds. "```

For anybody interested, there are different ways to count date differences between US and Europe, and there was quite a thread about that last year : https://forum.xojo.com/15374-getting-the-date-difference-in-years-months-days/0

[quote]Dim dtStartTime As New Date(2014, 12, 24, 00, 00, 00) // YYY, MM, DD, HH, MN, SS
Dim dtNow As New Date
Dim i As double = dtNow.TotalSeconds - dtStartTime.TotalSeconds
Dim dtresult As New Date
dtresult.TotalSeconds = i
MsgBox str(max(0,dtresult.year-1905))+" Years, “+str(dtresult.month)+” Months, “+str(dtresult.Day)+” Days, " _
+str(dtresult.Hour)+" Hours, " +str(dtresult.Minute)+" Minutes, " +str(dtresult.Second)+" Seconds. "[/quote]
This code failed under xojo 2015, Date is only supported as Date(2014, 12, 24,TimeZone.Current)
I would like to test this code between two event like (2014, 12, 24, 00, 00, 00), (2014, 12, 23, 00, 30, 00) to get a diff in minutes (enough).