要求按标准日历方式输出指定年月的日历样式
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
| 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.DateTimedt=System.DateTime.Now;
2
dt=dt.AddDays(-(dt.Day-1));
3
intthisMonth=dt.Month;
4
System.Console.WriteLine(dt.ToLongDateString());
5
char[]weekChar="日一二三四五六".ToCharArray();
6
for(inti=0;i<=weekChar.Length-1;i++)
7

{
8
System.Console.Write("{0,3}",weekChar[i]);
9
}
10
System.Console.WriteLine();
11
for(inti=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();
System.DateTimedt=System.DateTime.Now;2
dt=dt.AddDays(-(dt.Day-1));3
intthisMonth=dt.Month;4
System.Console.WriteLine(dt.ToLongDateString());5
char[]weekChar="日一二三四五六".ToCharArray();6
for(inti=0;i<=weekChar.Length-1;i++)7


{8
System.Console.Write("{0,3}",weekChar[i]);9
}10
System.Console.WriteLine();11
for(inti=0;i<=(int)dt.DayOfWeek-1;i++)12


{13
System.Console.Write("{0,4}","");14
}15
do16


{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();
本文介绍了一段使用C#编程语言实现控制台输出指定月份日历样式的代码。该程序通过DateTime类获取当前日期,并向前推算到当月的第一天,然后输出星期标签并填充日期至月末。
2822

被折叠的 条评论
为什么被折叠?



