Results 1 to 7 of 7

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

  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.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

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

    Those last two If statements aren't really necessary. The statement:

    pfSeconds = Int(pfSeconds Mod 60)

    should NEVER set pfSeconds to 60. I'm not certain that the same is true for the minutes, but the calculation of minutes and hours can be re-written using the Mod function such that the first if statement will never be needed. Unfortunately, re-writing the first two lines seems unlikely to provide ANY increase in speed, so there is no point in doing so unless the current implementation makes the first If necessary.

    As to there being some greater .NET functionality, I don't know of any offhand, but maybe somebody else does.
    My usual boring signature: Nothing

  3. #3

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

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

    Like you said, using Mod is do-able (I just revised what was there) but it really doesn't make it any more "Dot Netty".

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

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

    Well, what exactly is "Dot Netty"

    One of the biggest changes from VB6 to .NET is that .NET is more OO. The concept behind OO is encapsulating data and the functions that work on the data such that an object can be dropped into code with the understanding that it will operate as expected without external code.

    You have found some functionality which would make sense in an object. If you were to create a class that took a time, or a number of seconds in constructors, and exposed properties or methods that returned whatever, you would be able to drop that class into any code where you needed it. That would be fundamentally in the spirit of OO, and would be fundamentally in the spirit of .NET, so it would be "Dot Netty".

    Of course, that sounds a little like the existing TimeSpan class, so you might look at that to see whether there is anything there which would be of use.
    My usual boring signature: Nothing

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

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

    Have a look at the member listing of the TimeSpan structure. It has Days, Hours, Minutes, Seconds, TotalDays, TotalHours, TotalMinutes, TotalSeconds and more. Here's an illustration of the difference between the properties that start with "Total" and those that don't. If a TimeSpan represented 90 seconds then here's what the value of a few properties would be:

    Minutes: 1
    Seconds: 30
    TotalMinutes: 1.5
    TotalSeconds: 90

    To get the string you want you can just do this:
    VB Code:
    1. Dim ts As TimeSpan = Date.Now.TimeOfDay
    2. Dim str As String = String.Format("{0:d2}:{1:d2}:{2:d2}", _
    3.                                   Convert.ToInt32(Math.Floor(ts.TotalHours)), _
    4.                                   ts.Minutes, _
    5.                                   ts.Seconds)
    6.  
    7. MessageBox.Show(str)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

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

    Messing about with intellisense I came up with:
    VB Code:
    1. Dim t As Date = Date.MinValue
    2. t = t.AddSeconds(9999)
    3. Dim span As TimeSpan = t - Date.MinValue
    4. Dim ts As New TimeSpanConverter
    5. Me.Text = ts.ConvertToInvariantString(span)

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

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

    Nice work jo0ls. I didn't know about that class myself. It's a member of the System.ComponentModel namespace. The one drawback of that method is that it will include milliseconds, but it's just as easy or easier to remove them as it is to use my method or any other to format the time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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