Solved

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

Posted on 2006-10-31
8
2,797 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

630 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