要求按标准日历方式输出指定年月的日历样式
日 | 一 | 二 | 三 | 四 | 五 | 六 |
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
1 System.DateTime dt = System.DateTime.Now;
2 dt = dt.AddDays(-(dt.Day - 1));
3 int thisMonth = dt.Month;
4 System.Console.WriteLine(dt.ToLongDateString());
5 char[] weekChar = "日一二三四五六".ToCharArray();
6 for (int i = 0; i <= weekChar.Length - 1; i++)
7 {
8 System.Console.Write("{0,3}", weekChar[i]);
9 }
10 System.Console.WriteLine();
11 for (int i = 0; i <= (int)dt.DayOfWeek - 1; i++)
12 {
13 System.Console.Write("{0,4}", " ");
14 }
15 do
16 {
17 System.Console.Write("{0,4}", dt.Day);
18
19 if (dt.DayOfWeek == System.DayOfWeek.Saturday)
20 {
21 System.Console.WriteLine("");
22 }
23 dt = dt.AddDays(1);
24 }
25 while (dt.Month == thisMonth);
26 System.Console.WriteLine();
2 dt = dt.AddDays(-(dt.Day - 1));
3 int thisMonth = dt.Month;
4 System.Console.WriteLine(dt.ToLongDateString());
5 char[] weekChar = "日一二三四五六".ToCharArray();
6 for (int i = 0; i <= weekChar.Length - 1; i++)
7 {
8 System.Console.Write("{0,3}", weekChar[i]);
9 }
10 System.Console.WriteLine();
11 for (int i = 0; i <= (int)dt.DayOfWeek - 1; i++)
12 {
13 System.Console.Write("{0,4}", " ");
14 }
15 do
16 {
17 System.Console.Write("{0,4}", dt.Day);
18
19 if (dt.DayOfWeek == System.DayOfWeek.Saturday)
20 {
21 System.Console.WriteLine("");
22 }
23 dt = dt.AddDays(1);
24 }
25 while (dt.Month == thisMonth);
26 System.Console.WriteLine();
本文转自shyleoking 51CTO博客,原文链接:http://blog.51cto.com/shyleoking/806891