convert time

BlitzPlus Forums/BlitzPlus Programming/convert time

Paulo(Posted 2004) [#1]
Can anyone help me with this please? I'm trying to convert a given amount of seconds into HH:MM:SS format. Converting from to seconds is easy, but back again is giving me a hard time. I did a search online and found some VB code, (posted below) but I can't get my head around converting it to BB.


Public Function ConvertSeconds(lSeconds As Long) As String

    'Declare temporary variables
    Dim lTmpMinutes As Long
    Dim lTmpSeconds As Long
    Dim lTmpHours As Long
    
    'Do we even need to do all this math for nothin'?
    If lSeconds > 59 Then
        'Get's *just* the amount of seconds that will be returned
        lTmpSeconds = lSeconds Mod 60
        'Do we have more than an hour's worth of seconds?
        If lSeconds > 3599 Then
            'How many hours are we talking about
            lTmpHours = Fix(lSeconds / 3600)
  
           'Oooh fun. Take the original seconds and figure out
            'how many minutes we have.  Then remove from that
            'anything that says "60" for the minutes.  So we
             'don't have "2:60:05"
 
           lTmpMinutes = lSeconds / 60 - (60 * lTmpHours)

            'Finally return the value all nicely formatted up
            ConvertSeconds = lTmpHours & ":" & Format _
           (lTmpMinutes, "00") & ":" & Format(lTmpSeconds, "00")
        Else
            'Ok.  We've got something less than an hour, whew!
            'How many minutes do we have?
            lTmpMinutes = Fix(lSeconds / 60)
            
             'Seconds were already figured above (lSeconds Mod
             '60) so we just return the value and we be done.
            ConvertSeconds = lTmpMinutes & ":" & _
                 Format(lTmpSeconds, "00")
        End If
    Else
        'This is for the lazy that sends us less than even a
        '*minutes* worth of seconds.   Just dress it up and send
        'it back.
        ConvertSeconds = "0:" & Format(lSeconds, "00")
    End If

End Function




Ryan Moody(Posted 2004) [#2]
This should do it:

HH=SS/3600
MM=(SS Mod 3600)/60
SS=(SS Mod 3600) Mod 60

Ryan


Paulo(Posted 2004) [#3]
Thanks Ryan.


Regular K(Posted 2004) [#4]
Lol, all that VB code, then 3 lines of BB code does it.


Eikon(Posted 2004) [#5]
The same three lines will work in VB too, the previous function just formats the output into a single string.