We help IT Professionals succeed at work.

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

on
Medium Priority
2,897 Views
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

Eric

Comment
Watch Question

## View Solutions Only

Commented:
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)

Not the solution you were looking for? Getting a personalized solution is easy.

Architect

Commented:
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
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

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

Commented:
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?

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

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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...

Commented:
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
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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
Access more of Experts Exchange with a free account
##### Thanks for using Experts Exchange.

Limited access with a free account allows you to:

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile