Results 1 to 7 of 7

Thread: [RESOLVED] [2005] Convert Seconds -> Hrs : Mins : Secs - Is There a .NET way?

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Resolved [RESOLVED] [2005] Convert Seconds -> Hrs : Mins : Secs - Is There a .NET way?

    I need to be able to convert elapsed seconds to hours, minutes and seconds.

    Wandering about the web, I found a VB6 solution to the problem.
    I modified it to work in .NET thus:


    Code in Timer:
    VB Code:
    1. Static stop_time As DateTime
    2.         Dim elapsed_time As TimeSpan
    3.  
    4.         ' build elapsed time for display
    5.         stop_time = Now
    6.         elapsed_time = stop_time.Subtract(start_time)
    7.  
    8.         lblElapsed.Text = TimeString(elapsed_time.TotalSeconds)
    Code in Function:
    VB Code:
    1. Public Function TimeString(ByVal afSeconds As Double) As String
    2.         Dim pfHrs As Double
    3.         Dim pfMinutes As Double
    4.         Dim pfSeconds As Double
    5.  
    6.         pfSeconds = afSeconds
    7.  
    8.         pfHrs = Int(pfSeconds / 3600)
    9.         pfMinutes = (Int(pfSeconds / 60)) - (pfHrs * 60)
    10.         pfSeconds = Int(pfSeconds Mod 60)
    11.  
    12.         If pfSeconds = 60 Then
    13.             pfMinutes += 1
    14.             pfSeconds = 0
    15.         End If
    16.  
    17.         If pfMinutes = 60 Then
    18.             pfMinutes = 0
    19.             pfHrs += 1
    20.         End If
    21.  
    22.         Return pfHrs.ToString("00") & ":" & pfMinutes.ToString("00") & ":" & pfSeconds.ToString("00")
    23.  
    24.     End Function
    I suppose I could be happy with this, but I would think that this age-old problem has probably been solved more eloquently with the dawn of the .NET Framework.

    Anyone know how?
    Last edited by rjbudz; May 22nd, 2006 at 05:36 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width