Results 1 to 4 of 4

Thread: converting seconds to minutes/seconds

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2001
    Posts
    302

    converting seconds to minutes/seconds

    I have a var that holdes a number of seconds. I would like to convert that into minutes and seconds...
    EX: myVar = 255 seconds... I would like to do an MSGBOX where the result would be: 4 minutes and 15 seconds...

    any ideas?

    thanks

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    VB Code:
    1. Dim minsec As String
    2. minsec = CStr(myVar) \ 60 & ":" & CStr(myVar Mod 60)
    3. MsgBox minsec

  3. #3
    Addicted Member Michael Woolsey's Avatar
    Join Date
    Nov 2000
    Location
    Calgary, Alberta, Canada.
    Posts
    243
    Here is a small function that I have used to do this:

    VB Code:
    1. Public Function ConvertSeconds(ByVal sngSeconds As Single) As String
    2.    Dim intHours As Integer
    3.    Dim intMinutes As Integer
    4.    Dim intSeconds As Integer
    5.    Dim strOutString As String
    6.    
    7.    intHours = sngSeconds \ (60 * 60)
    8.    intMinutes = (sngSeconds \ 60) - (intHours * 60)
    9.    intSeconds = sngSeconds Mod 60
    10.    
    11.    strOutString = ""
    12.    If intHours <> 0 Then
    13.       If intHours = 1 Then
    14.          strOutString = intHours & " hour, "
    15.       Else
    16.          strOutString = intHours & " hours, "
    17.       End If
    18.    End If
    19.    
    20.    If intMinutes <> 0 Then
    21.       If intMinutes = 1 Then
    22.          strOutString = strOutString & intMinutes & " minute, "
    23.       Else
    24.          strOutString = strOutString & intMinutes & " minutes, "
    25.       End If
    26.      
    27.    End If
    28.    
    29.    If intSeconds <> 0 Then
    30.       If intSeconds = 1 Then
    31.          strOutString = strOutString & intSeconds & " second."
    32.       Else
    33.          strOutString = strOutString & intSeconds & " seconds."
    34.       End If
    35.    End If
    36.    
    37.    If Len(strOutString) <= 0 Then
    38.       'Less than a second.
    39.       strOutString = "1 second."
    40.    End If
    41.    
    42.    ConvertSeconds = strOutString
    43. End Function
    Granted it doesn't go to days, but if you need that, I'm sure you can get it modified to do what you want.

    Edit: I just noticed this code will leave a , at the end if it's zero seconds, easy enough to fix if you want though.

    Hope this helps.
    Michael
    Last edited by Michael Woolsey; Mar 4th, 2003 at 02:01 PM.
    Application/Web Developer

    Visual Basic 6.0 SP5
    Active Server Pages
    Oracle 9i
    - I'm going to live forever, or die trying!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2001
    Posts
    302
    thank you

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