How to display updated time as system time on shortcut using C #?

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

+7
source share
7 answers

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

this.label1.Text = DateTime.Now.ToString(); 
+12
source

Add a Timer element that fires once per second (1000 ms). In this tick event timer, you can update your tag with the current time.

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

+8
source

You can add a timer element and specify it for a 1000 millisecond interval

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

Try using the following code:

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

You must also enable the timer, either in code or in the properties window.

in the code, enter the following in the form download section:

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

After that, make sure your timer event looks like this:

 private void myTimer_Tick(object sender, EventArgs e) { timeLabel.Text = DateTime.Now.ToString("hh:mm:ss"); } 
+4
source

Since the timer interval is not accurate, your update may be poorly synchronized and will drift relative to the actual seconds transition. On some events, you will lag either before the transition and skip updates in your time display

Instead of polling at a high frequency to start updating when seconds change, this method can give you some respect.

If you like the knobs, you can set your time update to be safe 100 ms after the actual second jump by adjusting the 1000 ms timer using the Millisecond property of the time stamp you want to display.

In the timer event code, do something like this:

 //Read time DateTime time = DateTime.Now; //Get current ms offset from prefered readout position int diffms = time.Millisecond-100; //Set a new timer interval with half the error applied timer.Interval = 1000 - diffms/2; //Update your time output here.. 

Then the timer interval should then approach the selected point 100 ms after the transition seconds. When during the transition + 100 ms the error will switch +/- keeping the reading position in time.

0
source
 private int hr, min, sec; public Form2() { InitializeComponent(); hr = DateTime.UtcNow.Hour; min = DateTime.UtcNow.Minute; sec = DateTime.UtcNow.Second; } //Time_tick click private void timer1_Tick(object sender, EventArgs e) { hr = DateTime.UtcNow.Hour; hr = hr + 5; min = DateTime.UtcNow.Minute; sec = DateTime.UtcNow.Second; if (hr > 12) hr -= 12; if (sec % 2 == 0) { label1.Text = +hr + ":" + min + ":" + sec; } else { label1.Text = hr + ":" + min + ":" + sec; } } 
0
source

All Articles