|
-
May 22nd, 2006, 05:20 PM
#1
Thread Starter
Hyperactive Member
[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:
Static stop_time As DateTime
Dim elapsed_time As TimeSpan
' build elapsed time for display
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
lblElapsed.Text = TimeString(elapsed_time.TotalSeconds)
Code in Function:
VB Code:
Public Function TimeString(ByVal afSeconds As Double) As String
Dim pfHrs As Double
Dim pfMinutes As Double
Dim pfSeconds As Double
pfSeconds = afSeconds
pfHrs = Int(pfSeconds / 3600)
pfMinutes = (Int(pfSeconds / 60)) - (pfHrs * 60)
pfSeconds = Int(pfSeconds Mod 60)
If pfSeconds = 60 Then
pfMinutes += 1
pfSeconds = 0
End If
If pfMinutes = 60 Then
pfMinutes = 0
pfHrs += 1
End If
Return pfHrs.ToString("00") & ":" & pfMinutes.ToString("00") & ":" & pfSeconds.ToString("00")
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.
-
May 22nd, 2006, 05:59 PM
#2
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
 
-
May 22nd, 2006, 06:23 PM
#3
Thread Starter
Hyperactive Member
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".
-
May 22nd, 2006, 06:29 PM
#4
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
 
-
May 22nd, 2006, 06:30 PM
#5
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:
Dim ts As TimeSpan = Date.Now.TimeOfDay
Dim str As String = String.Format("{0:d2}:{1:d2}:{2:d2}", _
Convert.ToInt32(Math.Floor(ts.TotalHours)), _
ts.Minutes, _
ts.Seconds)
MessageBox.Show(str)
-
May 22nd, 2006, 06:33 PM
#6
Hyperactive Member
Re: [2005] Convert Seconds -> Hrs : Mins : Secs - Is There a .NET way?
Messing about with intellisense I came up with:
VB Code:
Dim t As Date = Date.MinValue
t = t.AddSeconds(9999)
Dim span As TimeSpan = t - Date.MinValue
Dim ts As New TimeSpanConverter
Me.Text = ts.ConvertToInvariantString(span)
-
May 22nd, 2006, 06:56 PM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|