Solved

How do I convert seconds (decimal) to Hours:Minutes:Seconds.0000

Posted on 2006-10-31
8
2,787 Views
Last Modified: 2011-09-20
Friends,

I have a value, obtained from a string, and converted to decimal.  Now, an example of the decimal value would be 4321.1234.  I want to convert that decimal value to Hours:Minutes:Seconds.1234.  How do I do that?

Example 4321.1234 would be converted to 1:12:01.1234

Syntax help please!

Thanks in advance!

Eric

0
Comment
Question by:indy500fan
8 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
ID: 17843065
See TimeSpan structure. TimeSpan instance can be initialized with number of seconds, and then printed in desired format using ToString method. However, TimeSpan presision is milliseconds, which is not enough for you. You can pass only 4321 to TimeSpan constructor, format it and then add .1234.

You can initilaize TimeSpan instance using constructor
Dim instance As New TimeSpan(hours, minutes, seconds)

with parameters (0, 0, nimber_of_seconds)
0
 
LVL 12

Expert Comment

by:Mamtha1982
ID: 17843109
try this

Public Function SecToTime(Seconds As Double) As String
    Dim   H, M As Integer
    Dim S As Double
    H= Seconds/3600
   M = Seconds/60 - H * 60
   S = Seconds - (H * 3600 + M * 60)
   Return H.Tostring & ":" & M.ToString & ":" & S.Tostring
End Function
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17843236
Another version using TimeSpan:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim timeInSecs As Double = 4321.1234
        Dim strTime As String = ConvertTimeInSeconds(timeInSecs)
        Debug.WriteLine(strTime)
    End Sub

    Private Function ConvertTimeInSeconds(ByVal timeInSeconds As Double) As String
        Dim ts As New TimeSpan(timeInSeconds * TimeSpan.TicksPerSecond)
        Return ts.Hours & ":" & ts.Minutes.ToString("00") & ":" & ts.Seconds.ToString("00") & "." & ts.Milliseconds.ToString("0000")
    End Function
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:indy500fan
ID: 17843477
Idle_Mind,

I think AlexFM is onto something with regards to Milliseconds.  Yours works great, except that the Milliseconds value is returned as .0123

Is there a trick to the Milliseconds?
0
 

Author Comment

by:indy500fan
ID: 17843633
I think I have it based on a combination of your responses, but I wonder if you guys can improve upon it.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim TimeString As String = "4321.1234"
        Dim SplitUpTimeString() As String
        SplitUpTimeString = Split(TimeString, ".")
        Dim timeInSeconds As Double = SplitUpTimeString(0)
        Dim strTime As String = ConvertTimeInSeconds(timeInSeconds) & "." & SplitUpTimeString(1)
        MessageBox.Show(strTime)

    End Sub

    Private Function ConvertTimeInSeconds(ByVal timeInSeconds As Double) As String
        Dim ts As New TimeSpan(timeInSeconds * TimeSpan.TicksPerSecond)
        Return ts.Hours & ":" & ts.Minutes.ToString("00") & ":" & ts.Seconds.ToString("00")
    End Function

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17843911
Shouldn't it just be?

    Return ... & "." & ts.Milliseconds.ToString("000")

Note that ToString("0000") has been changed to ToString("000") since Milliseconds can be between 0 and 999.

I put an extra zero in there by accident...
0
 

Author Comment

by:indy500fan
ID: 17843926
Idle Mind,

Uh, well, that will only give me to the thousandths place right?  I need it to the ten-thousandths place (our timing system is accurate to the ten-thousandths place.  :)

Eric
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 450 total points
ID: 17844226
I think this can handle a wider range of inputs (with or without the fractional seconds):

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim timeInSecs As Double = 4321.1234
        Dim strTime As String = ConvertTimeInSeconds(timeInSecs)
        Debug.WriteLine(strTime)
    End Sub

    Private Function ConvertTimeInSeconds(ByVal timeInSeconds As Double) As String
        Dim ts As New TimeSpan(timeInSeconds * TimeSpan.TicksPerSecond)
        Dim retVal As String = ts.Hours & ":" & ts.Minutes.ToString("00") & ":" & ts.Seconds.ToString("00") & "."

        Dim milliSeconds As String = "0000"
        Dim strTimeInSeconds As String = timeInSeconds.ToString
        If strTimeInSeconds.Contains(".") Then
            milliSeconds = strTimeInSeconds.Substring(strTimeInSeconds.IndexOf(".") + 1).PadRight(4, "0")
            If milliSeconds.Length > 4 Then
                milliSeconds = milliSeconds.Substring(0, 4)
            End If
        End If

        Return retVal & milliSeconds
    End Function
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question