Time Between Dates

By | 2019-08-24

Calculate the time elapsed between two dates.

Option Explicit
Public Function ElapsedTime(tStart, tStop) As String
' *******************************************************************
' Function Name : ElapsedTime *
' Created By : Herry Hariry Amin *
' Email : h2arr@cbn.net.id *
' Language : VB4, VB5, VB6 *
' Example : sYourVariable = ElapsedTime(tStartTime,tStopTime) *
' *******************************************************************

   Dim dtr, dtl, jml As Long

   dtl = (Hour(tStart) * 3600) + (Minute(tStart) * 60) + (Second(tStart))
   dtr = (Hour(tStop) * 3600) + (Minute(tStop) * 60) + (Second(tStop))
   If tStop < tStart Then
      jml = 86400
   Else
      jml = 0
   End If
   jml = jml + (dtr - dtl)
   ElapsedTime = Format(Str(Int((Int((jml / 3600)) Mod 24))), "00") + ":" + Format(Str(Int((Int((jml / 60)) Mod 60))), "00") + ":" + Format(Str(Int((jml Mod 60))), "00")
End Function

Usage

Private Sub Command1_Click()
   MsgBox ElapsedTime(Now, Now - 0.512)
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.