Results 1 to 15 of 15

Thread: converting seconds

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    converting seconds

    How would I turn a number in seconds into the normal time format of 0:00:00:000 (hours:minutes:seconds:milliseconds)

    So, if the number of seconds was 125 then the time would be
    0:02:05
    Then show it without the erroneous numbers to make
    2:05

    Another problem is to show the 0 when the seconds is less than ten.
    So, instead of 0:3 show 0:03

    Sorry if I have confused any of you. thanks.
    Prefix has no suffix, but suffix has a prefix.

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: converting seconds

    something like this?:
    VB Code:
    1. Private Function ConvertSeconds(ByVal lSec As Long) As String
    2.     ConvertSeconds = Format$(lSec \ 3600, "00") & ":" & _
    3.                      Format$((lSec Mod 3600) \ 60, "00") & ":" & _
    4.                      Format$(lSec Mod 60, "00")
    5. End Function

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: converting seconds

    How about TimeSerial() function:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim iSeconds As Long
    3.  
    4.     iSeconds = 7276
    5.     Debug.Print Format(Hour(TimeSerial(0, 0, iSeconds)), "00") & _
    6.                 ":" & _
    7.                 Format(Minute(TimeSerial(0, 0, iSeconds)), "00") & _
    8.                 ":" & _
    9.                 Format(Second(TimeSerial(0, 0, iSeconds)), "00")
    10.  
    11. End Sub
    Output should be: 02:01:16

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: converting seconds

    Probably wouldn't be an issue, but the TimeSerial method is limted to 9hrs, 6mins and 7secs or so.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: converting seconds

    Where did you get those numbers from?
    The actual limitations are the arguments (hour,minute,second) which are defined as Integers so if you pass any greater number you'll facwe the "overflow" error... But in most cases as you said it "wouldn't be an issue".

  6. #6

  7. #7

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: converting seconds

    eh?

    I know what causes the error.

    i didn't mean the TimeSerial function itself was limited to 9hrs, 6mins and 7secs, just that if Troy is going to use the TimeSerial method in the form you suggested then he'd better be aware that it can't evaluate time periods longer than 9hrs, 6mins and 7secs.

  9. #9

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: converting seconds

    Calm down a little.

    I use this:

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Label1.Caption = ConvertMilliSeconds(7600)
    4.  
    5. End Sub
    6.  
    7. Private Function ConvertMilliSeconds(ByVal mSec As Long) As String
    8.     ConvertMilliSeconds = Format$(mSec \ 3600000, "00") & ":" & _
    9.                      Format$((mSec \ 60000), "00") & ":" & _
    10.                      Format$(mSec \ 1000, "00") & "." & _
    11.                      Format$(mSec Mod 1000, "000")
    12. End Function

    Now, there is one thing that I wanted to comment on. I noticed that with the code above, as is, Label1 displays 00:00:07.600. However, if I were to change the "\" sign for seconds to the "/" sign, it would be 00:00:08.600.

    Why is that?
    Prefix has no suffix, but suffix has a prefix.

  10. #10
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: converting seconds

    \ is integer division, so 7600 \ 1000 = 7
    / is normal division, so 7600 / 1000 = 7.6

    the format function performs rounding, so you end up with 8 instead of 7.

    your function won't work for a couple of reasons (try putting in 77600 for example). Try this:
    VB Code:
    1. Private Function ConvertMilliSeconds(ByVal mSec As Long) As String
    2.     ConvertMilliSeconds = Format$(mSec \ 3600000, "00") & ":" & _
    3.                      Format$((mSec Mod 3600000) \ 60000, "00") & ":" & _
    4.                      Format$((mSec Mod 60000) / 1000, "00.000")
    5. End Function

  11. #11

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: converting seconds

    Ok, I rewrote it like so:

    VB Code:
    1. Private Function cSeconds(ByVal mSec As Long) As String
    2. mSec = 30000
    3.     Select Case mSec
    4.        
    5.         Case Is >= 600, Is < 3600
    6.             cSeconds = _
    7.             Format$((mSec Mod 3600) \ 60, "00") & ":" & _
    8.             Format$(mSec Mod 60, "00")
    9.         Case Is < 600
    10.             cSeconds = "0:" & _
    11.             Format$(mSec Mod 60, "00")
    12.         Case Is >= 3600, Is < 36000
    13.             cSeconds = _
    14.             Format$(mSec \ 3600, "00") & ":" & _
    15.             Format$((mSec Mod 3600) \ 60, "00") & ":" & _
    16.             Format$(mSec Mod 60, "00")
    17.     End Select
    18.    
    19. End Function

    Now there is one problem. With mSec at 30000 it completely ignores the first part of the string (the hours) and it returns "20:00". What is going on? No matter what number I make mSec, it will not show the hours. I tried 3599 which returned "59:59" then 3600 which returned "00:00" then 3601 which returned "00:01"

    Why is it not showing the hours?
    Prefix has no suffix, but suffix has a prefix.

  12. #12
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: converting seconds

    well if msec is greater than 600, it'll go into the first case.

    i missed off a zero in my code, in case you hadn't noticed (which I've now changed)

  13. #13
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: converting seconds

    Edit Perhaps do this:
    VB Code:
    1. Private Function ConvertMilliSeconds(ByVal mSec As Long) As String
    2.     If mSec >= 3600000 Then ConvertMilliSeconds = Format$(mSec \ 3600000, "00") & ":"
    3.     If mSec >= 60000 Then ConvertMilliSeconds = ConvertMilliSeconds & Format$((mSec Mod 3600000) \ 60000, "00") & ":"
    4.     ConvertMilliSeconds = ConvertMilliSeconds & Format$((mSec Mod 60000) / 1000, "00.000")
    5. End Function

  14. #14
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: converting seconds

    Adding up into the chaos!

    VB Code:
    1. Public Type HourMinuteSecondMS
    2.     Hours As Integer
    3.     Mins As Byte
    4.     Secs As Byte
    5.     MS As Integer
    6. End Type
    7.  
    8. Public Function GetHMSfromMS(ByVal Milliseconds As Long) As HourMinuteSecondMS
    9.     GetHMSfromMS.Hours = CInt(Milliseconds \ 3600000)
    10.     GetHMSfromMS.Mins = CByte((Milliseconds Mod 3600000) \ 60000)
    11.     GetHMSfromMS.Secs = CByte((Milliseconds Mod 60000) \ 1000)
    12.     GetHMSfromMS.MS = CInt(Milliseconds Mod 1000)
    13. End Function
    14. Public Function GetStrFromMS(ByVal Milliseconds As Long, Optional ByVal AlwaysFull As Boolean) As String
    15.     Dim lngHours As Long, lngMins As Long, lngSecs As Long, lngMS As Long
    16.     Dim blnHours As Long, blnMins As Long
    17.     lngHours = Milliseconds \ 3600000
    18.     blnHours = (lngHours > 0) Or AlwaysFull
    19.     lngMins = (Milliseconds Mod 3600000) \ 60000
    20.     blnMins = (lngMins > 0) Or blnHours
    21.     lngSecs = (Milliseconds Mod 60000) \ 1000
    22.     lngMS = Milliseconds Mod 1000
    23.     If blnHours Then
    24.         GetHMSfromMS = CStr(lngHours) & ":" & Format$(lngMins, "00") & ":" & _
    25.             Format$(lngSecs, "00") & "." & Format$(lngMS, "000")
    26.     ElseIf blnMins Then
    27.         GetHMSfromMS = Format$(lngMins, "00") & ":" & _
    28.             Format$(lngSecs, "00") & "." & Format$(lngMS, "000")
    29.     Else
    30.         GetHMSfromMS = Format$(lngSecs, "00") & "." & Format$(lngMS, "000")
    31.     End If
    32. End Function

    These are two different kinds of function. The other returns, at minimum, 00.000, but it automatically adds minutes or hours if necessary, so you end up with 00:00.000 or 0:00:00.000 formatted strings.

    GetHMSfromMS instead returns a udt which contains all values splitted up nicely. This is useful if you want to make a custom digital display or something of that kind instead of using a label.

  15. #15

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: converting seconds

    Quote Originally Posted by bushmobile
    well if msec is greater than 600, it'll go into the first case.
    You are quite right. I didn't even notice it. I put the
    VB Code:
    1. Case Is >= 3600
    at the top and it works now.

    Thank you all for your help.
    Prefix has no suffix, but suffix has a prefix.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width