钟表计时器

来源:互联网 发布:java根据模板生成pdf 编辑:程序博客网 时间:2024/04/27 23:46

钟表计时器

实现代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace _2{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        public void GetTime()        {            DateTime time = DateTime.Now;            int year = time.Year;            int month = time.Month;            int day = time.Day;            int hour = time.Hour;            int h1 = 0;            int h2 = hour; ;            if (hour >= 10)            {                h1 = hour / 10;                h2 = hour % 10;            }            int minute = time.Minute;            int m1 = 0;            int m2 = minute;            if (minute >= 10)            {                m1 = minute / 10;                m2 = minute % 10;            }            int second = time.Second;            int s1 = 0;            int s2 = second;            if (second >= 10)            {                s1 = second / 10;                s2 = second % 10;            }            label1.Text = string.Format("{0}{1}:{2}{3}:{4}{5}", h1, h2, m1, m2, s1, s2);//label控件显示时间            label2.Text = string.Format("{0}年{1}月{2}日",year ,month,day);        }        private void timer1_Tick(object sender, EventArgs e)        {           // label2.Text = DateTime.Now;            this.GetTime();        }        private void Form1_Load(object sender, EventArgs e)        {            this.timer1.Interval = 1000;            this.timer1.Start();        }    }}