No announcement yet.

Time$ to Seconds

  • Filter
  • Time
  • Show
Clear All
new posts

  • Time$ to Seconds

    I'm trying to convert the Time$ string to a long representing the # of seconds so I can do comparisons of elapsed seconds easily. Essentially:

    lSeconds = Hours * 3600 + Minutes *60 + Seconds

    I have come up with a solution but it might not be the best solution. My solution is as follows:

        sTime = Time$
        sSecs = Right$(sTime,2)
        ' Does Time$'s hours field always fill with a leading zero?
        if Len(sTime) = 8 then
            sHours = Left$(sTime,2) 
            sMins = Mid$(sTime, 4,2)
        Else 'Hour is a single digit
            sHours = Left$(sTime,1)
            sMins = Mid$(sTime, 3,2)
        End If
        ' 48 is the ASCII for "0" (Zero)
        ' Convert the sSeconds, sMinutes and sHours to lSeconds
        lS = (CVByt(Left$(sSecs,1),1)-48)*10 + CVByt(Right$(sSecs,1),1)-48
        lM = ((CVByt(Left$(sMins,1),1)-48)*10 + CVByt(Right$(sMins,1),1)-48)*60
        lH = ((CVByt(Left$(sHours,1),1)-48)*10 + CVByt(Right$(sHours,1),1)-48)*3600
        lTime(ub) = lS + lM + lH
    How does this code look and is there a far better way to turn the Time$ to seconds? I am doing this in the 16 bit compiler if that matters.

    Patrick White

    [This message has been edited by Patrick White (edited June 22, 2001).]

  • #2
    originally posted by patrick white:
    i'm trying to convert the time$ string to a long representing the # of seconds so i can do comparisons of elapsed seconds easily. essentially:
    how does this code look and is there a far better way to turn the time$ to seconds? i am doing this in the 16 bit compiler if that matters.

    i posted this one a few days ago in this forum , check out

    function seconds( strtime as string) as quad
    'convert "hh:mm:ss" to seconds
    function = val(parse$(strtime,":",1))*3600 _
    + val(parse$(strtime,":",2))*60 _
    + val(parse$(strtime,":",3))
    end function

    (i used quad because we were converting full dates to seconds in another part)

    check out my free software at written in pb/dll)


    • #3
      Hi Patrick,

      The way I handle this is to simply add text zeros to the left of the string and then just take the RIGHT$ number of digits for example;

      TimeStr = RIGHT$("000"+FORMAT$(Time+Adjust), 4)

      All arithmetic is done with time - the number of minutes since midnight. Then it is conveted back to HH:MM 24hr time. This allows time to be treated as base 10.

      Time is a LONG varies from 0 to 1440 minutes so if it is a single digit you will get 0001 if it 2 digits you still get 0011.

      If I were to worl with seconds I would probably convert time to the number of seconds since Midnight and do the arithmetic.

      this is the function I use for converting Time to minutes:

      IntPortion = INT(Time\100)
      FracPortion = Time MOD 100
      TimeToMinutes = INT(60 * IntPortion + FracPortion )

      and back with:

      Hr = INT(MinsSent\60)
      Mins = MinsSent MOD 60
      MinutesToTime = INT(100 * Hr + Mins)

      Kind Regards


      • #4
        Thanks Stuart for your Seconds function. It gave me the general idea on how to solve it even though there is no PARSE$ function in 16 bit PB/DLL.

        Thanks to you also Mike for your help. All I can say is ICK to TS time functions.

        The function I am using is pasted below.

        FUNCTION Seconds(strTime AS STRING) AS LONG
        	'convert "hh:mm:ss" to seconds
         Dim sHours$, sMinutes$, sSeconds$, sTime$
        	sTime = strTime
        	sHours = Extract$(sTime, ":")
        	sTime = Mid$(sTime, Len(sHours) + 2)	'Account for the : Plus one
        	sMinutes = Extract$(sTime, ":")
           	sSeconds = Remove$(sTime, sMinutes & ":")
        	Function = Val(sHours)*3600 + Val(sMinutes)*60 + Val(sSeconds)
        ' No Parse$ function in 16 bit 
        '	FUNCTION = VAL(PARSE$(strTime,":",1))*3600 + VAL(PARSE$(strTime,":",2))*60 + VAL(PARSE$(strTime,":",3))
        Patrick White