运行结果:

【代码】:

  

 1 private void timer1_Tick(object sender, EventArgs e)
 2         {
 3             //获取当前年部分
 4             label3.Text =Convert.ToString( DateTime.Now.Year);
 5             //获取当前月部分
 6             label4.Text = Convert.ToString(DateTime.Now.Month);
 7             //获取当前日部分
 8             label6.Text = Convert.ToString(DateTime.Now.Day);
 9             //获取当前星期部分
10             string x = Convert.ToString(DateTime.Now.DayOfWeek);
11             switch (x)
12             {
13                 case "Monday":
14                     label9.Text = "";
15                     break;
16                 case "Tuesday":
17                     label9.Text = "";
18                     break;
19                 case "Wednesday":
20                     label9.Text = "";
21                     break;
22                 case "Thursday":
23                     label9.Text = "";
24                     break;
25                 case "Friday":
26                     label9.Text = "";
27                     break;
28                 case "Saturday":
29                     label9.Text = "";
30                     break;
31                 case "Sunday":
32                     label9.Text = "";
33                     break;
34             }
35             //获取当前小时的部分,来判断是上午还是下午
36             if (DateTime.Now.Hour >= 12)
37             {
38                 label12.Text = "下午";
39             }
40             else {
41                 label12.Text = "上午";
42             }
43 
44             if (sender == timer1)
45             {
46                 label1.Text = gettime();
47             }
48         }
49 
50         private string gettime()
51         {
52             //创建一个接收时间的字符串变量
53             string timestring = "";
54             //获取当前时间的时部分
55             int hour = DateTime.Now.Hour;
56             //获取当前时间的分部分
57             int min = DateTime.Now.Minute;
58             //获取当前时间的秒部分
59             int sec = DateTime.Now.Second;
60 
61             timestring=((hour<10)?"0"+hour.ToString():hour.ToString());
62             timestring += ":" + ((min < 10) ? "0" + min.ToString() : min.ToString());
63             timestring+=":"+((sec<10)?"0"+sec.ToString():sec.ToString());
64 
65             return timestring;
66         }

【重点在】:datetime.now.***;//获取当前时间的某部分

posted on 2016-01-12 15:48  海绵全是洞  阅读(283)  评论(0编辑  收藏  举报