B4J Question Time in seconds between two times formatted 'HHmmss' as strings

Discussion in 'B4J Questions' started by ElliotHC, Aug 12, 2019.

  1. ElliotHC

    ElliotHC Active Member Licensed User

    I need to get the integer difference in seconds between two time strings.

    They are formatted like this.

    HHmmss (e.g. 081256)

    I have two times and I need the difference in seconds between the two string times.
  2. emexes

    emexes Well-Known Member Licensed User

    If you need to account for summer time / daylight saving then consider DateUtils library PeriodBetween, but otherwise consider this:
    Sub SecondsBetween(T1 As String, T2 As StringAs Int
    If T1.Length <> 6 Or T2.Length <> 6 Then
    Return 0    'dude, what are you doing?!?!
        End If

    Dim H1 As Int = T1.SubString2(02)
    Dim M1 As Int = T1.SubString2(24)
    Dim S1 As Int = T1.SubString2(46)

    Dim H2 As Int = T2.SubString2(02)
    Dim M2 As Int = T2.SubString2(24)
    Dim S2 As Int = T2.SubString2(46)

    Dim T1Seconds As Int = H1 * 3600 + M1 * 60 + S1
    Dim T2Seconds As Int = H2 * 3600 + M2 * 60 + S2

    Return (T2Seconds - T1Seconds + 86400Mod 86400    'works across midnight, eg 233000 to 003000 should be 1 hour = 3600 seconds

    End Sub
    Last edited: Aug 12, 2019
    Peter Simpson likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice