|
-
Jan 15th, 2009, 06:46 AM
#1
Thread Starter
Lively Member
How do you change the format of a TimeSpan property?
I have the following code, and I want it to act as a stopwatch.
Code:
Dim spn As New TimeSpan(0, 0, 0)
Private Sub elapsedtime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles elapsedtime.Tick
spn = spn.Add(New TimeSpan(0, 0, 1))
Label6.Text = String.Format("{1}:{2}", spn.Hours, spn.Minutes, spn.Seconds)
End Sub
However, when the "stopwatch" ticks, the time is shown like this - 0:3, 2:9, etc. How can I change it so that when the seconds are in the single digits, it shows the time like a normal clock (4:05, 2:09, etc.)?
-
Jan 15th, 2009, 07:06 AM
#2
Re: How do you change the format of a TimeSpan property?
Can you not just do this instead:
vb Code:
Label6.Text = (spn.ToString)
Then if you dont want the hours bit included you could just remove all characters up to the first : (and the : itself of course).
There might be a better way but thats all I could come up with I'm afraid
-
Jan 15th, 2009, 07:37 AM
#3
Re: How do you change the format of a TimeSpan property?
What Chris says is correct but to answer your question specifically:
vb.net Code:
Label6.Text = String.Format("{1}:{2:00}", spn.Hours, spn.Minutes, spn.Seconds)
I would ask though, why are you passing Hours, Minutes and Seconds and only using the Minutes and Seconds? Why aren't you doing this:
vb.net Code:
Label6.Text = String.Format("{0}:{1:00}", spn.Minutes, spn.Seconds)
Also, rather than do this:
vb.net Code:
spn = spn.Add(New TimeSpan(0, 0, 1))
I'd sooner do this:
vb.net Code:
spn = spn.Add(TimeSpan.FromSeconds(1.0))
-
Jan 15th, 2009, 03:53 PM
#4
Thread Starter
Lively Member
Re: How do you change the format of a TimeSpan property?
Alright I got the format right when it looks like (minutes:seconds) but how do I get hours in there so it looks like (hours:minutes:seconds). Here's the code I have:
Code:
Dim spn As New TimeSpan(0, 0, 0)
Private Sub elapsedtime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles elapsedtime.Tick
spn = spn.Add(New TimeSpan(0, 0, 1))
Label6.Text = "elapsed time: " & String.Format("{00}:{1:00}", spn.Minutes, spn.Seconds)
End Sub
Last edited by nickf77; Jan 15th, 2009 at 03:57 PM.
-
Jan 15th, 2009, 04:45 PM
#5
Re: How do you change the format of a TimeSpan property?
If you want hours, minutes and seconds then your format string should be "{0}:{1:00}:{2:00}" and you must follow it with the Hours, Minutes and Seconds properties.
-
Jan 15th, 2009, 04:50 PM
#6
Re: How do you change the format of a TimeSpan property?
I'm sure that would work but would it not be simpler to just use the TimeSpan.ToString() method I mentioned earlier as thats exactly what that outputs?
So just:
vb.net Code:
Label6.Text = (spn.ToString)
-
Jan 15th, 2009, 05:42 PM
#7
Thread Starter
Lively Member
Re: How do you change the format of a TimeSpan property?
I actually chose to use chris128's method which was a lot easier and now it works. Thanks both of you though.
-
Jan 15th, 2009, 07:12 PM
#8
Re: How do you change the format of a TimeSpan property?
 Originally Posted by nickf77
I actually chose to use chris128's method which was a lot easier and now it works. Thanks both of you though.
:O I suppose I should savor this moment as its probably going to be the only time anyone is ever going to use a suggestion I have made over one of yours JMC
Now... to catch up with your 4.5 billion Rep points
-
Jan 15th, 2009, 07:31 PM
#9
Re: How do you change the format of a TimeSpan property?
 Originally Posted by chris128
:O I suppose I should savor this moment as its probably going to be the only time anyone is ever going to use a suggestion I have made over one of yours JMC
Now... to catch up with your 4.5 billion Rep points 
You'll note that I did talk up your suggestion in my first post, but went on to show how you could use a format string to get a specific format. You'd only do that if you didn't actually want the format that ToString produces though. There's no point doing more work than is needed. The OP's original post didn't indicate specifically whether they want hours, minutes and seconds or not, so it's best to be aware of both options.
-
Jan 15th, 2009, 07:35 PM
#10
Re: How do you change the format of a TimeSpan property?
I know I'm only kidding but yeah your right it is best to explain other options... your post taught me something about String.Format that I didnt know before anyway so its all good.
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
|