c#实现输出本月的月历
程序员文章站
2023-12-19 18:33:34
格式要求:复制代码 代码如下:su mo tu we th fr sa 01...
格式要求:
复制代码 代码如下:
su mo tu we th fr sa
01 02 03 04
05 06 07 08 09 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
代码:
复制代码 代码如下:
class interview1
{
static void main()
{
printcalender(2011, 10);
}
public static void printcalender(int year, int month)
{
formatdate fd = new formatdate(year, month);
string calender =
@"su mo tu we th fr sa
{0} {0} {0} {0} {0} {0} {0} {0}
{0} {0} {0} {0} {0} {0} {0} {0}
{0} {0} {0} {0} {0} {0} {0} {0}
{0} {0} {0} {0} {0} {0} {0} {0}
{0} {0} {0} {0} {0} {0} {0} {0}";
calender = string.format(calender, fd).trimend();
console.writeline(calender);
}
}
class formatdate : iformattable
{
int num;
int max;
public formatdate(int year, int month)
{
datetime dt = new datetime(year, month, 1);
num = (int)dt.dayofweek * -1;
max = datetime.daysinmonth(year, month);
}
public string tostring(string format,iformatprovider formatprovider)
{
return num++ < 0 || num > max ? " " : num.tostring("00");
}
}