Converting HH:MM:SS Into Seconds (And Reversed!)-VBForums
Results 1 to 7 of 7

Thread: Converting HH:MM:SS Into Seconds (And Reversed!)

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    226

    Converting HH:MM:SS Into Seconds (And Reversed!)

    Hello!
    I wrote a little program to convert HH:MM:SS into Seconds. Here is the code:

    vb Code:
    1. Private Function HMSToSeconds(ByRef HMS As String) As Int32
    2.  
    3.         Try
    4.             Dim Str As String = HMS
    5.             Dim ColonCount As Int32 = 0
    6.             For i As Int32 = 0 To Str.Count - 1 Step 1
    7.                 If Str(i) = ":"c Then
    8.                     ColonCount += 1
    9.                 End If
    10.             Next
    11.             If ColonCount <= 2 Then
    12.                 Dim StrArray() As String = Str.Split(":"c)
    13.                 Dim Sec As Int32 = 0
    14.                 Dim Factor As Int32 = 1
    15.                 For i As Int32 = StrArray.Count - 1 To 0 Step -1
    16.                     Sec += StrArray(i) * Factor
    17.                     Factor *= 60
    18.                 Next
    19.                 Return Sec
    20.             Else
    21.                 MsgBox("Incorrect format. Use HH:MM:SS")
    22.             End If
    23.         Catch ex As Exception
    24.             MsgBox(ex.Message)
    25.         End Try
    26.  
    27.     End Function

    I also did the reverse: (Seconds into HH:MM:SS)

    vb Code:
    1. Private Function SecondsToHMS(ByRef Seconds As Int32) As String
    2.  
    3.         Try
    4.             Dim Sec As Int32 = Seconds
    5.             Dim Factor As Int32 = 60 ^ 2
    6.             Dim HMS As String = Nothing
    7.             Dim Counter As Int32 = 0
    8.             For i As Int32 = 2 To 0 Step -1
    9.                 Counter = 0
    10.                 Factor = 60 ^ i
    11.                 Do Until Sec < Factor
    12.                     Sec -= Factor
    13.                     Counter += 1
    14.                 Loop
    15.                 If Counter < 10 Then
    16.                     HMS += "0"
    17.                 End If
    18.                 HMS += Counter.ToString
    19.                 If i > 0 Then
    20.                     HMS += ":"
    21.                 End If
    22.             Next
    23.             Return HMS
    24.         Catch ex As Exception
    25.             MsgBox(ex.Message)
    26.         End Try
    27.  
    28.     End Function

    I hope you can find a use for this.
    ~NinjaNic

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,598

    Re: Converting HH:MM:SS Into Seconds (And Reversed!)

    Or:
    vb.net Code:
    1. Private Function HMSToSeconds(hms As String) As Int32
    2.     Dim seconds As TimeSpan
    3.  
    4.     If TimeSpan.TryParseExact(hms, "hh\:mm\:ss", Nothing, seconds) Then
    5.         Return Convert.ToInt32(seconds.TotalSeconds)
    6.     Else
    7.         'Invalid format.
    8.     End If
    9. End Function
    10.  
    11. Private Function SecondsToHMS(seconds As Int32) As String
    12.     Return TimeSpan.FromSeconds(seconds).ToString("hh\:mm\:ss")
    13. End Function
    Note that the method parameters are not declared ByRef, as there's no good reason to do so.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    226

    Re: Converting HH:MM:SS Into Seconds (And Reversed!)

    Wow, I didn't know about this TimeSpan class. Guess I should do my studying.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,598

    Re: Converting HH:MM:SS Into Seconds (And Reversed!)

    Quote Originally Posted by NinjaNic View Post
    I didn't know about this TimeSpan class.
    It's actually a structure.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Lively Member
    Join Date
    Aug 2011
    Posts
    66

    Re: Converting HH:MM:SS Into Seconds (And Reversed!)

    Thats a very useful sample. Thanks!

  6. #6

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    226

    Re: Converting HH:MM:SS Into Seconds (And Reversed!)

    I prefer my code because HMSToSeconds can work in either format HH:MM:SS / MM:SS and the Hour part can have as many digits as needed.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,923

    Re: Converting HH:MM:SS Into Seconds (And Reversed!)

    I guess you could always put your code in the "Else" case, if the TimeSpan.TryParse can't handle the format.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.