How to display updated time as system time on a label using c#?

67,465

Solution 1

Add a new Timer control to your form, called Timer1, set interval to 1000 (ms), then double click on the Timer control to edit the code-behind for Timer1_Tick and add this code:

this.label1.Text = DateTime.Now.ToString();

Solution 2

You can Add a timer control and specify it for 1000 millisecond interval

  private void timer1_Tick(object sender, EventArgs e)
    {
        lblTime.Text = DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss tt");
    }

Solution 3

Add a Timer control that is set to fire once every second (1000 ms). In that timer's Tick event, you can update your label with the current time.

You can get the current time using something like DateTime.Now.

Solution 4

Try the following code:

private void timer1_Tick(object sender, EventArgs e)
{
    lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
}

Solution 5

You must set the timer to be enabled also, either in code, or in the properties window.

in code, please type the following in the form load section:

myTimer.Enabled = true; 
myTimer.Interval = 1000;

After that, make sure your timer event is similar to this:

private void myTimer_Tick(object sender, EventArgs e)
{
    timeLabel.Text = DateTime.Now.ToString("hh:mm:ss");            
}
Share:
67,465
Admin
Author by

Admin

Updated on July 22, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to display current time on a label using C# but time will continuously change as system time changes. How can I do this?

  • Tarık Özgün Güner
    Tarık Özgün Güner about 9 years
    And dont forget starting the timer with Timer1.Start();
  • servermanfail
    servermanfail about 9 years
    Good catch - or set the Enabled property in the designer to True
  • Jonathan Mee
    Jonathan Mee over 7 years
    Code only answers are considered poor practice. Please include a quick blurb of explanation.