Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Convert Seconds into HH:MM:SS

Posted on 2007-07-25
13
Medium Priority
?
4,003 Views
Last Modified: 2008-01-09
I need a function that converts seconds into hours, minutes and seconds.  The issue is, I have a number that is something like 2473.86 seconds.

So, 2473.86 / 60 = 41.231 minutes (.231 being the percentage of the next minute).

So, it's actually 0 hours, 41 minutes and 14 seconds (13.86 seconds to be precise)

I'm using VB.Net Framework 2.
0
Comment
Question by:IUFITS
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 3

Author Comment

by:IUFITS
ID: 19568157
Or, convert 155.24 to to HH:MM:SS where it would be 02:35:14
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 19568176
Use the timespan class:
   
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Test As Double = 2473.86
        Dim ts As New TimeSpan(CLng(Test * 10000000))
        MsgBox(ts.ToString)
        MsgBox(String.Format("Hours: {0}, Minutes {1}, Seconds {2}", ts.Hours, ts.Minutes, ts.Seconds))


    End Sub
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 total points
ID: 19568211
Try...

        Dim seconds As Decimal = 2473.86
        Dim ts As New TimeSpan(TimeSpan.TicksPerSecond * seconds)
        Dim strSpan As String = ts.Hours & ":" & ts.Minutes.ToString("00") & ":" & ts.Seconds.ToString("00") & "." & ts.Milliseconds.ToString("000")
        Debug.Print(strSpan)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19568220
Ooops...sorry PaulHews...I stepped away from the computer there.  It's basically the same thing as yours isn't it?   ;)
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 19568267
Yep, good old timespan.  :)
0
 
LVL 2

Expert Comment

by:drekow
ID: 19568450
Timespan works great for this and with the given seconds one could write a function that returns a broader amount of options (decades, years, weeks, months, etc...).
0
 
LVL 3

Author Comment

by:IUFITS
ID: 19569361
Close, but it doesn't seem to work all the time.  It could be my mistake with my other code...  

Distance: 112.538882169675
Nobleman Time in Minutes Per Square: 35
Total Minutes: 3938.860875938625

Formatted Hours with the below function: 17:38:51.652

    Public Function TravelTime(ByVal Speed As UnitSpeed) As String        
        Dim Minutes As Decimal = Distance() * Speed
        Dim Ts As New TimeSpan(TimeSpan.TicksPerMinute * Minutes)
        Dim strSpan As String = Ts.Hours.ToString() & ":" & Ts.Minutes.ToString("00") & ":" & Ts.Seconds.ToString("00") & "." & Ts.Milliseconds.ToString("000")
        Return strSpan
    End Function
0
 
LVL 3

Author Comment

by:IUFITS
ID: 19569376
It should come back with 68:35:52 for the above example but instead it comes back with 17:38:51.
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 1800 total points
ID: 19569437
Use this:

Dim strSpan As String = Ts.TotalHours.ToString() & ":" & Ts.Minutes.ToString("00") & ":" & Ts.Seconds.ToString("00") & "." & Ts.Milliseconds.ToString("000")
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 19569460
The Hours method will return the hours for the day.  Since you spanned 2 days, 17 hours and change, you were only seeing the 17 hours.
0
 
LVL 3

Author Comment

by:IUFITS
ID: 19569529
Awesome, that explains it and helps me understand how it works.  Perfect.  Now, I was still getting one more hour than expected in some cases... but I figured it was because the hours were rounding up and since I was showing the minutes, I didn't want the hours to round up.  So, I fixed it using the 'Fix' command.  Here's the syntax...  if I'm incorrect let me know.  I'm going to go ahead and accept your answer though.  Thanks a ton for the prompt help!  :)

    Public Function TravelTime(ByVal Speed As UnitSpeed) As String        
        Dim Minutes As Decimal = Distance() * Speed
        Dim Ts As New TimeSpan(TimeSpan.TicksPerMinute * Minutes)        
        Dim strSpan As String = Fix(Ts.TotalHours).ToString("00") & ":" & Ts.Minutes.ToString("00") & ":" & Ts.Seconds.ToString("00") & "." & Ts.Milliseconds.ToString("000")
        Return strSpan
    End Function
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 19569615
Yes, TotalHours is a double that can contain fractional hours.  So you are right, you need to use Fix to truncate it.
0
 
LVL 3

Author Comment

by:IUFITS
ID: 19569626
You've been very helpful.  Thanks again Paul!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

572 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