Results 1 to 10 of 10

Thread: How do you change the format of a TimeSpan property?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    80

    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.)?

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How do you change the format of a TimeSpan property?

    Can you not just do this instead:
    vb Code:
    1. 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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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

    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:
    1. 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:
    1. Label6.Text = String.Format("{0}:{1:00}", spn.Minutes, spn.Seconds)
    Also, rather than do this:
    vb.net Code:
    1. spn = spn.Add(New TimeSpan(0, 0, 1))
    I'd sooner do this:
    vb.net Code:
    1. spn = spn.Add(TimeSpan.FromSeconds(1.0))
    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    80

    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.

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

    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.
    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
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. Label6.Text = (spn.ToString)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    80

    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.

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How do you change the format of a TimeSpan property?

    Quote 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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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

    Re: How do you change the format of a TimeSpan property?

    Quote 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.
    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

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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