Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-10-31
8
Medium Priority
?
2,803 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 200 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:Praveen Kumar
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 86

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 86

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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1800 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

885 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