获取某月的开始日期和结束日期
1获取某月的开始日期和结束日期#region 获取某月的开始日期和结束日期
2 /**//// <summary>
3 /// 获取某月的开始日期和结束日期
4 /// </summary>
5 /// <param name="year">年份</param>
6 /// <param name="month">月份</param>
7 /// <param name="dtStart">开始日期</param>
8 /// <param name="dtEnd">结束日期</param>
9 public static void GetSedtOfMonth(int year, byte month, out DateTime dtStart, out DateTime dtEnd)
10 {
11 // 获取本月的天数
12 byte days = GetDaysOfMonth(year, month);
13 // 返回本月的开始日期和结束日期
14 dtStart = new DateTime(year, month, 1);
15 dtEnd = new DateTime(year, month, days);
16 }
17 #endregion
2 /**//// <summary>
3 /// 获取某月的开始日期和结束日期
4 /// </summary>
5 /// <param name="year">年份</param>
6 /// <param name="month">月份</param>
7 /// <param name="dtStart">开始日期</param>
8 /// <param name="dtEnd">结束日期</param>
9 public static void GetSedtOfMonth(int year, byte month, out DateTime dtStart, out DateTime dtEnd)
10 {
11 // 获取本月的天数
12 byte days = GetDaysOfMonth(year, month);
13 // 返回本月的开始日期和结束日期
14 dtStart = new DateTime(year, month, 1);
15 dtEnd = new DateTime(year, month, days);
16 }
17 #endregion
1获取某月的天数#region 获取某月的天数
2 /**//// <summary>
3 /// 获取某月的天数
4 /// </summary>
5 /// <param name="year">年份</param>
6 /// <param name="month">月份</param>
7 /// <returns>天数</returns>
8 public static byte GetDaysOfMonth(int year, byte month)
9 {
10 byte days = 31;
11 switch(month)
12 {
13 case 1:
14 days = 31;
15 break;
16 case 3:
17 goto case 1;
18 case 5:
19 goto case 1;
20 case 7:
21 goto case 1;
22 case 8:
23 goto case 1;
24 case 10:
25 goto case 1;
26 case 12:
27 goto case 1;
28 case 4:
29 days = 30;
30 break;
31 case 6:
32 goto case 4;
33 case 9:
34 goto case 4;
35 case 11:
36 goto case 4;
37 case 2:
38 if ((year % 4 == 0 && year % 100 != 0) || year % 400 ==0)
39 {
40 days = 29;
41 }
42 else
43 {
44 days = 28;
45 }
46 break;
47 default:
48 days = 31;
49 break;
50 }
51 return days;
52 }
53 #endregion
2 /**//// <summary>
3 /// 获取某月的天数
4 /// </summary>
5 /// <param name="year">年份</param>
6 /// <param name="month">月份</param>
7 /// <returns>天数</returns>
8 public static byte GetDaysOfMonth(int year, byte month)
9 {
10 byte days = 31;
11 switch(month)
12 {
13 case 1:
14 days = 31;
15 break;
16 case 3:
17 goto case 1;
18 case 5:
19 goto case 1;
20 case 7:
21 goto case 1;
22 case 8:
23 goto case 1;
24 case 10:
25 goto case 1;
26 case 12:
27 goto case 1;
28 case 4:
29 days = 30;
30 break;
31 case 6:
32 goto case 4;
33 case 9:
34 goto case 4;
35 case 11:
36 goto case 4;
37 case 2:
38 if ((year % 4 == 0 && year % 100 != 0) || year % 400 ==0)
39 {
40 days = 29;
41 }
42 else
43 {
44 days = 28;
45 }
46 break;
47 default:
48 days = 31;
49 break;
50 }
51 return days;
52 }
53 #endregion