Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2003
    Location
    Europe
    Posts
    14

    Unanswered: Formatting Seconds value to a Time value

    Hi there,

    Does anyone know how I could format a value vSeconds, say = 72000, which represents seconds into a Time value, vTime which should equal in this case 02:00:00?

  2. #2
    Join Date
    Nov 2002
    Location
    Belgium
    Posts
    243

    Re: Formatting Seconds value to a Time value

    I found this on the Internet.
    I don't think there's an easier way to do this.



    ' Purpose : Converts a long value in milliseconds to a corresponding
    ' Hours:Minuteseconds string.
    '
    ' Accepts : lngTimeInMilliSeconds
    '
    ' Returns : A string which is in the format 'HH:MMS' for the exam
    time.


    Public Function Milli2HMS(ByVal lngTimeInMilliSeconds As Long) As String

    Dim lngSecRemainder As Long
    Dim lngMinSecRemainder As Long
    Dim lngHoursPart As Long
    Dim lngMinutesPart As Long
    Dim lngSecondsPart As Long
    Dim sTimeRemaining As String
    Dim sHoursPart As String
    Dim sMinutesPart As String
    Dim sSecondsPart As String


    lngHoursPart = lngTimeInMilliSeconds \ 3600000
    lngMinSecRemainder = lngTimeInMilliSeconds Mod 3600000
    lngMinutesPart = lngMinSecRemainder \ 60000
    lngSecRemainder = lngMinSecRemainder Mod 60000
    lngSecondsPart = lngSecRemainder \ 1000

    sHoursPart = Format(lngHoursPart, "00")
    sMinutesPart = Format(lngMinutesPart, "00")
    sSecondsPart = Format(lngSecondsPart, "00")

    sTimeRemaining = sHoursPart & ":" & sMinutesPart & ":" & sSecondsPart


    'or more efficient but longer
    ' sTimeRemaining = Space(8)
    ' Mid$(sTimeRemaining, 1, 2) = sHoursPart
    ' Mid$(sTimeRemaining, 3, 1) = ":"
    ' Mid$(sTimeRemaining, 4, 2) = sMinutesPart
    ' Mid$(sTimeRemaining, 6, 1) = ":"
    ' Mid$(sTimeRemaining, 7, 2) = sSecondsPart

    Milli2HMS = sTimeRemaining


    End Function

  3. #3
    Join Date
    Jan 2003
    Location
    Europe
    Posts
    14

    Re: Formatting Seconds value to a Time value

    Yip, I used something similar as follows

    (vSeconds / 3600) + ':' + ((vSeconds % 3600) / 60) + ':' + ((vSeconds % 3600) % 60)

    It worked a treat, but be careful when using floats, always covert to an int before division etc ....

Posting Permissions

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