要求按标準月曆方式輸出指定年月的月曆樣式
日
一
二
三
四
五
六
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

System.DateTime dt = System.DateTime.Now;

dt = dt.AddDays(-(dt.Day - 1));

int thisMonth = dt.Month;

System.Console.WriteLine(dt.ToLongDateString());

char[] weekChar = "日一二三四五六".ToCharArray();

for (int i = 0; i <= weekChar.Length - 1; i++)
{
System.Console.Write("{0,3}", weekChar[i]);
}

System.Console.WriteLine();

for (int i = 0; i <= (int)dt.DayOfWeek - 1; i++)
System.Console.Write("{0,4}", " ");

do
System.Console.Write("{0,4}", dt.Day);
if (dt.DayOfWeek == System.DayOfWeek.Saturday)
System.Console.WriteLine("");
dt = dt.AddDays(1);

while (dt.Month == thisMonth);

本文轉自shyleoking 51CTO部落格,原文連結:http://blog.51cto.com/shyleoking/806891