Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.Format of a timer?

Tags:

c#

I've a time ticker event, I want to write it to a label in format ( hours:minutes:seconds 00:00:00 ) it does not print the 0 values! it shows like ::1 when starts to count... what to do? Solved, thanks for all replies

private void timer_Tick(object sender, EventArgs e)
        {
            seconds++;
            if(seconds == 59)
            {
                minutes++;
                seconds = 0;
            }
            if(minutes == 59)
            {
                hours++;
                minutes = 0;
            }

            this.label1.Text = string.Format("{0:##}:{1:##}:{2:##}", hours, minutes, seconds);
        }
like image 775
anarhikos Avatar asked Sep 05 '25 16:09

anarhikos


1 Answers

A better method is using DateTime and TimeSpan objects. For example:

DataTime start = <set this somehow>

void timer_Tick(...)
{
   var elapsed = DateTime.Now - start;

   label1.Text = string.Format("{0:HH:mm:ss}", elapsed);
}
like image 122
sukru Avatar answered Sep 07 '25 17:09

sukru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!