Results 1 to 6 of 6

Thread: How to get Mins and Seconds from seconds left.

  1. #1

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Resolved How to get Mins and Seconds from seconds left.

    Hi there,

    How do i convert a number which is in seconds (eg 310 seconds) to 5 minutes 10 seconds?

    Do i use the mod function (which i cant seem to be able to use in the pocket pc development.,.)
    Last edited by dinosaur_uk; Apr 27th, 2005 at 02:44 AM.

  2. #2
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: How to get Mins and Seconds from seconds left.

    here is one i used before:

    VB Code:
    1. Public Function formatLength(ByVal howManySeconds As Object) As Object
    2.  
    3.         Dim mySeconds As Object
    4.         Dim myHours As Object
    5.         Dim myMinutes As Object
    6.  
    7.         On Error GoTo errCheck '//just incase of an error
    8.  
    9.         ' //duration '
    10.         mySeconds = howManySeconds
    11.  
    12.         'if mySeconds is greater or equal to 3,600 seconds
    13.  
    14.         If mySeconds >= 3600 Then
    15.             'get hours which is equal to seconds divided by 3600
    16.             myHours = mySeconds / 3600
    17.  
    18.             'set the seconds to the numbers after the decimal sign
    19.             'thats what mod does
    20.             mySeconds = mySeconds Mod 3600
    21.         Else
    22.             'if not greater than 3600, just set it to 0
    23.             myHours = 0
    24.         End If
    25.  
    26.         If mySeconds >= 60 Then
    27.             'greater than or equal to 60
    28.             'set the minutes equal to the value of (seconds divided by 60).
    29.             'and get the remaining numbers after the decimal
    30.             'which will be the seconds
    31.             'using the mod sign
    32.  
    33.             myMinutes = mySeconds \ 60
    34.             mySeconds = mySeconds Mod 60
    35.         Else
    36.             'if not set to 0
    37.             myMinutes = 0
    38.         End If
    39.  
    40.         'return
    41.         formatLength = Format$(myHours, "00:") & Format$(myMinutes, "00:") & Format$(mySeconds, "00")
    42.         Exit Function
    43.  
    44. errCheck:
    45.         formatLength = ""
    46.     End Function
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  3. #3
    Addicted Member New2VB's Avatar
    Join Date
    Jun 2000
    Location
    Los Angeles
    Posts
    161

    Re: How to get Mins and Seconds from seconds left.

    If you can't use Mod function in PPC development environ, wouldn't the following work:

    VB Code:
    1. Public Function ConvertRawSeconds(RawSeconds As Int) As String
    2.    Dim RemainingSecs As Integer
    3.    Dim Minutes As Integer
    4.    Dim Seconds As Integer
    5.  
    6.    RemainingSecs = RawSeconds
    7.    Minutes = 0
    8.    Seconds = 0
    9.  
    10.    Do While RemainingSecs > 0
    11.       If RemainingSecs > 59 Then
    12.          Minutes = Minutes + 1
    13.          RemainingSecs = RemainingSecs - 60
    14.       Else
    15.          Seconds = RemainingSecs
    16.          RemainingSecs = 0
    17.       End If
    18.    Loop
    19.    ' And borrowing/adapting syntax from Strider's previous posting
    20.    ConvertRawSeconds = Format$(Minutes, "00:") & Format$(Seconds, "00")
    21. End Function
    Obviously you could add one more If/Then switch to account for hours if needed.

  4. #4

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: How to get Mins and Seconds from seconds left.

    Found it !!

    Quote Originally Posted by wossname
    VB Code:
    1. Dim ts As TimeSpan = TimeSpan.FromSeconds(3931)       '3931 seconds
    2.     MessageBox.Show("Hours: " & ts.Hours)
    3.     MessageBox.Show("Minutes: " & ts.Minutes)
    4.     MessageBox.Show("Seconds: " & ts.Seconds)

  5. #5
    Addicted Member New2VB's Avatar
    Join Date
    Jun 2000
    Location
    Los Angeles
    Posts
    161

    Re: How to get Mins and Seconds from seconds left.

    That was certainly more elegant than my suggestion.

  6. #6

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: How to get Mins and Seconds from seconds left.

    Heee...i know....

    Thank goodness i checked the forums again...

    It was wossname who posted it.. i am just putting it here so pple knows it works on the CF.

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