public partial class Form1 : Form
{
public Form1()
{InitializeComponent();}
int year, month, day, weekday;
bool isleap;
string weekstr;
private void but1_Click(object sender, EventArgs e)
{
if (text1.Text.Trim() != "")
{ year = Convert.ToInt32(text1.Text.Trim());
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
{
isleap = true;
}
else
{
isleap = false;
}
}
if (text2.Text.Trim() != "")
{
month = Convert.ToInt32(text2.Text.Trim());
if (month <= 0 || month >= 12)
lab4.Text = "您输入的月份有误,请输入1到12月!";
}
if (text3.Text.Trim() != "")
{
day = Convert.ToInt32(text3.Text.Trim());
if ((month != 2) && (((month < 7) && (month % 2 == 1)) || (month > 7) && (month % 2 == 0)))
{
if (day <= 0 || day > 31)
{
l照起宽检按几致宽著木ab4.Text = "输入的日期有误,本月大,数据应在0-31!";
return;
}
}
else if ((month != 2) && ((矛倍天密加氢七任离(month < 7) && (month % 2 == 0)) | (mon厂政th > 7) && (month % 2 == 1)))
{
if (day <= 0 || day > 30)
{ lab4.Text = "输入的日期有误,本月小,数据应在0-30!"; return; }
}
else
{
if (isleap == true)
{
if (day <= 0 || day > 29)
{
lab4.Text = "输入错误,本年为闰年,应该是0-29!";
return;
}
else
{
if (day <= 0 || day > 28)
{
lab4.Text = "输入错误,本年为平年,应该是0-28!";
return;
}
}
}
}
if (month == 1 || month == 2)
{ month += 12; }
weekday = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
switch (weekday)
{
case 0: weekstr = "星期一"; break;
case 1: weekstr = "星期二"; break;
case 2: weekstr = "星期三"; break;
case 3: weekstr = "星期四"; break;
case 4: weekstr = "星期五"; break;
case 5: weekstr = "星期六"; break;
case 6: weekstr = "星期天"; break;
}
lab4.Text = text1.Text.Trim() + "年" + text2.Text.Trim() + "月" + text3.Text.Trim() + "日是" + weekstr + "。"; } }
}这个代码是做参考的,我自己测试2012后日期都判断错误了,这是我大学教科书上的,呵呵