此代码段是否可以缩短,以便仍显示当前时间
像这样:08:24:09?
而不是像8:24.9
我目前的代码:
this.lblClock = new System.Windows.Forms.Label();
this.lblClock.Text = "00:00:00";
时钟滴答声:
private void _Clock_Tick(object sender, EventArgs e)
{
DateTime CurrTime = DateTime.Now;
if (CurrTime.Hour > 9 && CurrTime.Minute > 9 && CurrTime.Second > 9)
lblClock.Text = ("" + CurrTime.Hour + ":" + CurrTime.Minute + ":" + CurrTime.Second);
if (CurrTime.Hour < 10 && CurrTime.Minute > 9 && CurrTime.Second > 9)
lblClock.Text = ("0" + CurrTime.Hour + ":" + CurrTime.Minute + ":" + CurrTime.Second);
if (CurrTime.Hour > 9 && CurrTime.Minute > 9 && CurrTime.Second < 10)
lblClock.Text = ("" + CurrTime.Hour + ":" + CurrTime.Minute + ":" + "0" + CurrTime.Second);
if (CurrTime.Hour < 10 && CurrTime.Minute > 9 && CurrTime.Second < 10)
lblClock.Text = ("0" + CurrTime.Hour + ":" + CurrTime.Minute + ":" + "0" + CurrTime.Second);
if (CurrTime.Hour > 9 && CurrTime.Minute < 10 && CurrTime.Second > 9)
lblClock.Text = ("" + CurrTime.Hour + ":" + "0" + CurrTime.Minute + ":" + CurrTime.Second);
if (CurrTime.Hour < 10 && CurrTime.Minute < 10 && CurrTime.Second > 9)
lblClock.Text = ("0" + CurrTime.Hour + ":" + "0" + CurrTime.Minute + ":" + CurrTime.Second);
if (CurrTime.Hour > 9 && CurrTime.Minute < 10 && CurrTime.Second < 10)
lblClock.Text = ("" + CurrTime.Hour + ":" + "0" + CurrTime.Minute + ":" + "0" + CurrTime.Second);
if (CurrTime.Hour < 10 && CurrTime.Minute < 10 && CurrTime.Second < 10)
lblClock.Text = ("0" + CurrTime.Hour + ":" + "0" + CurrTime.Minute + ":" + "0" + CurrTime.Second);
}
你能缩短那个片段吗?
答案 0 :(得分:3)
您可以使用日期时间格式"HH:mm:ss"
在标签中显示时间。
lblClock.Text = CurrTime.ToString("HH:mm:ss");
如果您想显示上午/下午,那么您可以这样做:
lblClock.Text = CurrTime.ToString("hh:mm:ss tt");
答案 1 :(得分:0)
你可以做各种格式!! ......包括"HH:mm:ss"
,这就是你所追求的。