C#计时器关键代码

//定义日期
        private DateTime datetime = DateTime.Parse("2010-09-30 20:42:00");

        private void tmr_Tick(object sender, EventArgs e)
        {
            //判断日期是否已经到了
            if (datetime < DateTime.Now)
            {
                this.tmrNow.Enabled = false;
                this.txtTime.Text = "时间到了!";

            }
            else
            {
                //计算时差
                TimeSpan ts = this.datetime - DateTime.Now;

    //整理格式,显示
                this.txtTime.Text = ts.Days.ToString().PadLeft(2, '0') + "天"
                    + ts.Hours.ToString().PadLeft(2, '0') + ":"
                    + ts.Minutes.ToString().PadLeft(2, '0') + ":"
                        + ts.Seconds.ToString().PadLeft(2, '0');
            }
        }

 

你可能感兴趣的:(C#计时器关键代码)