简体   繁体   English

如何使用c#在标签上显示更新的时间作为系统时间?

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

I want to display current time on a label using C# but time will continuously change as system time changes. 我想使用C#在标签上显示当前时间,但是时间会随着系统时间的变化而不断变化。 How can I do this? 我怎样才能做到这一点?

将一个新的Timer控件添加到窗体中,称为Timer1,将时间间隔设置为1000(ms),然后双击Timer控件以编辑Timer1_Tick的代码并添加以下代码:

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

Add a Timer control that is set to fire once every second (1000 ms). 添加一个设置为每秒触发一次(1000毫秒)的Timer控件。 In that timer's Tick event , you can update your label with the current time. 在该计时器的Tick事件中 ,您可以使用当前时间更新标签。

You can get the current time using something like DateTime.Now . 您可以使用类似DateTime.Now方法获取当前时间。

You can Add a timer control and specify it for 1000 millisecond interval 您可以添加一个计时器控件并将其指定为1000毫秒间隔

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

Try the following code: 尝试以下代码:

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

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");            
}

Since the timer interval is not exact your update could be in bad sync and will be drifting with respect to the actual seconds transition. 由于计时器间隔不正确,因此您的更新可能无法同步,并且会相对于实际秒数转换发生偏移。 At some events you will lag behind or before the transition and miss updates in your time display 在某些情况下,您将滞后或落后于转换,并错过时间显示中的更新

Instead of polling att high frequency to fire the update at the change of the seconds this method may grant you some respect. 此方法可能会给您一些尊重,而不是在几秒钟的更改后以高频率轮询一次来触发更新。

If you like regulators you can adjust your time update to be safely located 100 ms after the actual second transition by adjusting the 1000 ms timer using the Millisecond property of the timestamp you want to display. 如果您喜欢调节器,则可以通过使用要显示的时间戳的毫秒属性来调整1000 ms计时器,从而将时间更新调整为在实际第二次转换后100 ms安全地定位。

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..

Next timer interval should then trigger closer to the selected point 100 ms after the seconds transition. 然后,下一个计时器间隔应在秒数转换后100毫秒内触发接近所选点。 When at the Transition+100ms the error will toggle +/- keeping your readout position in time. 当处于过渡状态+ 100ms时,错误将切换+/-,以保持您的读数位置及时。

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;
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM