Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 474
Yuki Kutsuya Avatar asked Apr 04 '12 12:04

Yuki Kutsuya


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


2 Answers

Use a TimeSpan:

_Counter += 1;
labelUpTime.Text = TimeSpan.FromSeconds(_Counter).ToString();
like image 146
Henk Holterman Avatar answered Sep 30 '22 08:09

Henk Holterman


You could make it a TimeSpan (for that's what it is, a span of time), then format that:

labelUpTime.Text = TimeSpan.FromSeconds(_Counter).ToString();
like image 37
AKX Avatar answered Sep 30 '22 09:09

AKX