Home > How-To Library > Date & Time
How to find the difference between two "times" on the same day or on two different days...
**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * **************************************************************** 'i.e. time difference between: ' 11:30:00 on 03/01/01 ' 22:00:20 on 04/01/01 ' 'Please use only 24 hr. format Public Function TimeDiff(LesserTime As String, LesserTimeDate As String, GreaterTime As String, _ GreaterTimeDate As String) As String Dim Thrs As Single, Tmins As Single, Tsecs As Single, ReturnTime As String Dim DTcheck As Single DTcheck = DateDiff("d", LesserTimeDate, GreaterTimeDate) If DTcheck > 0 Then For i = 0 To DTcheck Hour(GreaterTime) = Hour(GreaterTime) + 24 Next ElseIf DTcheck < 0 Then TimeDiff = "ERROR" Exit Function End If Thrs = Hour(GreaterTime) - Hour(LesserTime) If Thrs > 0 Then Tmins = Minute(GreaterTime) - Minute(LesserTime) If Tmins < 0 Then Tmins = Tmins + 60 Thrs = Thrs - 1 End If ElseIf Thrs = 0 And Minute(GreaterTime) > Minute(LesserTime) Then Tmins = Minute(GreaterTime) - Minute(LesserTime) Else TimeDiff = "ERROR" Exit Function End If Tsecs = Second(GreaterTime) - Second(LesserTime) If Tsecs < 0 Then Tsecs = Tsecs + 60 If Tmins > 0 Then Tmins = Tmins - 1 Else Tmins = 59 End If TimeDiff = TimeSerial(Thrs, Tmins, Tsecs) End Function
If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.