DateTime小综合

Posted on 2018-11-06 00:00  努力成长静待花开  阅读(171)  评论(0编辑  收藏  举报

实现效果:

关键知识:

1》DateTime类的ToString()方法;

2》DateTime类的IsLeapYear();

3》DateTime类的DaysInMomth();

4》Datetime对象的AddDays()方法;

实现代码:

 1         private void button1_Click(object sender, EventArgs e)
 2         {   //当前周几
 3             label1.Text="今天是:"+DateTime.Now.ToString("dddd");
 4         }
 5 
 6         private void button2_Click(object sender, EventArgs e)
 7         {   //当前年天数
 8             label2.Text =
 9                 DateTime.IsLeapYear(int.Parse(DateTime.Now.ToString("yyyy")))
10                 == true ?"今年有:366天" : "今年有:365天";
11         }
12 
13         private void button3_Click(object sender, EventArgs e)
14         {   //当前月天数
15             label3.Text ="当月有:"+
16                 DateTime.DaysInMonth(DateTime.Now.Year,
17                     DateTime.Now.Month)+"";
18         }
19 
20         private void button4_Click(object sender, EventArgs e)
21         {   //前一天日期
22             label4.Text =
23                 DateTime.Now.AddDays(-1).ToString("yyyy/M/dd");
24         }