Solved

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

Posted on 2006-10-31
8
2,782 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:indy500fan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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.

762 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

10 Experts available now in Live!

Get 1:1 Help Now