Search code examples
c#timerformattingint

C# Timer counter in xx.xx.xx format


I have a counter that counts up every 1 second and add 1 to an int.

Question
How can I format my string so the counter would look like this:

00:01:23

Instead of:

123

Things I've tried
Things I've tried so far:

for (int i = 0; i < 1; i++)
        {
            _Counter += 1;
            labelUpTime.Text = _Counter.ToString();
        }

My timer's interval is set to: 1000 (so it adds 1 every second).
I did read something about string.Format(""), but I don't know if it is applicable.
Thanks if you can guide me through this :D!


Solution

  • Use a TimeSpan:

    _Counter += 1;
    labelUpTime.Text = TimeSpan.FromSeconds(_Counter).ToString();