C# – 2 digit number in c#

c

A need some help, I have a TextBlock that contains a string like this 00:00:00 And I want to create a timer that will count every second e.g.00:00:01 , 00:00:02 etc

So the stupid thing that I do is to take the value of the text box

string[] times = myTextbox.Text.Split(':');
int hours = Int32.Parse(times[0]);
int minutes = Int32.Parse(times[1]);
int seconds = Int32.Parse(times[2]);

Then I increase the right variable and finally I join them again and put them back in the textblock, BUT now my counter is like this: 0:0:1, 0:0:2, …

I know the problem, its very logical but my question is how can I solve it 🙂

Thank you very much.

Best Answer

string displayString = String.Format("{0:00}:{1:00}:{2:00}", 
 hours, minutes, seconds);

The part after the : is a format description. 00 means always use at least two positions and show an empty position as 0.

Related Topic