直接上代码
CalcAge(DateTime.Parse("10/13/1998"));
static void CalcAge(DateTime birthday)
{
// 获得当前日期
DateTime curDate = DateTime.Today;
// 检查出生日期是否有效
if(birthday > curDate)
{
Console.WriteLine("Birthday must be equal to or before today");
}
else
{
int age = curDate.Year - birthday.Year;
// 如果出生的月份比当前月份小,则说明还不满一岁,所以年龄减一
if (birthday.Month > curDate.Month)
{
age--;
}
Console.WriteLine("The person's age is: {0}", age);
}
}