timer format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jollywg
    New Member
    • Mar 2008
    • 158

    timer format

    I'm trying to run a timer for a game. Right now its showing HH;mm:ss:millis econds. I need to format it in HH:mm:ss. How can i do this with this code?


    namespace FinalProj
    {
    public partial class frmFinalProj : Form
    {
    DateTime start = DateTime.Now;

    ...

    private void timer1_Tick(obj ect sender, EventArgs e)
    {
    TimeSpan timeElapsed = (DateTime.Now - start);
    string formattedTime = timeElapsed.ToS tring();
    label1.Text = formattedTime;
    }

    Thanks for the help

    Matt
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Code:
    string formattedTime = timeElapsed.ToString("HH:mm:ss");
    use hh if you wat 01 to be shown as 1 !

    Comment

    • Jollywg
      New Member
      • Mar 2008
      • 158

      #3
      Originally posted by Shashi Sadasivan
      Code:
      string formattedTime = timeElapsed.ToString("HH:mm:ss");
      use hh if you wat 01 to be shown as 1 !

      Thanks for the reply, but when i put the ToString in there it gave me this error error CS1501: No overload for method 'ToString' takes '1' arguments
      what does this mean?

      Thanks

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Originally posted by Jollywg
        Thanks for the reply, but when i put the ToString in there it gave me this error error CS1501: No overload for method 'ToString' takes '1' arguments
        what does this mean?

        Thanks
        That would only work using a DateTime. You are using a TimeSpan. I did a little testing, and it seems that a timeSpan.ToStri ng() gives a format of "HH:mm:ss.milis econds" (note the decimal)

        So, if you do this:
        [code=cpp]
        TimeSpan timeElapsed = (DateTime.Now - start);
        char[] separator = { '.' };
        string[] timeStrings = timeElapsed.ToS tring().Split(s eparator);
        string formattedTime = timeStrings[0];
        [/code]

        You will parse the string around the decimal, and then just ignore it. Unless you want to round it, in which case you could cast timeStrings[1] as an int and do your thing.

        Comment

        • Jollywg
          New Member
          • Mar 2008
          • 158

          #5
          Originally posted by insertAlias
          That would only work using a DateTime. You are using a TimeSpan. I did a little testing, and it seems that a timeSpan.ToStri ng() gives a format of "HH:mm:ss.milis econds" (note the decimal)

          So, if you do this:
          [code=cpp]
          TimeSpan timeElapsed = (DateTime.Now - start);
          char[] separator = { '.' };
          string[] timeStrings = timeElapsed.ToS tring().Split(s eparator);
          string formattedTime = timeStrings[0];
          [/code]

          You will parse the string around the decimal, and then just ignore it. Unless you want to round it, in which case you could cast timeStrings[1] as an int and do your thing.
          Thank you sooo much! It worked!

          Comment

          Working...