winform 和asp中好印象有专门的函数可以获取年月日
{
Console.Write("请选择年份: ");
int year = int.Parse(Console.ReadLine());
Console.Write("请选择月份: ");
int month = int.Parse(Console.ReadLine());
Console.WriteLine("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
int days = GetDaysOfMonth(year, month);
int firstDayOfMonth = GetWeekOfDate(year, month, 1);
for (int n = 0; n < firstDayOfMonth; n++)
{
Console.Write("\t");
}
for (int i = 1; i <= days; i++)
{
Console.Write(i + "\t");
if (GetWeekOfDate(year, month, i) == 6)
{
Console.WriteLine();
}
}
Console.ReadLine();
}
#region 判断是否为闰年
static bool IsleapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 100) || year % 400 == 0)
{
return true;
}
else
return false;
}
#endregion
#region 一个月有多少天
static int GetDaysOfMonth(int year, int month)
{
int days = 0;
switch (month)//定义月份
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = IsleapYear(year) ? 29 : 28;
break;
default:
days = 30;
break;
}
return days;
}
#endregion
#region 一年有多少天
static int GetDaysOfYear(int year)
{
return IsleapYear(year) ? 366 : 365;
}
#endregion
#region 时间的星期
static int GetWeekOfDate(int year, int month, int day)
{
int days = GetDaysOfDate(year, month, day);
return (days + 2) % 7;
}
#endregion
static int GetDaysOfDate(int year, int month, int day)
{
int totalDays = 0;
for (int i = 1980; i < year; i++)
{
totalDays = totalDays + GetDaysOfYear(i);
}
for (int i = 1; i < month; i++)
{
totalDays += GetDaysOfMonth(year, i);
}
totalDays = totalDays + day - 1;
return totalDays;
}
这个是我用控制台写的万年历 |