Solved

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

Posted on 2006-10-31
8
2,785 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

930 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now