try
{
Console.Write("请以年-月-日的形式输入您的生日:");
string strA = Console.ReadLine();
DateTime bir = Convert.ToDateTime(strA);
DateTime tod = DateTime.Today;
strA = bir.ToString("MM-dd");//将输入生日转换为月 日格式
string strB = tod.ToString("MM-dd");//今天月 日
bir = DateTime.Parse(strA);//再将生日字符串转换成日期
tod = DateTime.Parse(strB);
TimeSpan ti = bir - tod;//时间间隔
int j = ti.Days,k;
if (j >= 0)
{
if (j == 0)
Console.WriteLine("您今天生日,生日快乐!");
else
Console.WriteLine("您的生日距今还有"+j+"天");//还有多久生日
}
else
{
DateTime tod2 = tod.AddYears(1);
TimeSpan t2 = tod2 - tod;
k = t2.Days;
bir = bir.AddYears(1);
//TimeSpan t3 = tod2 - bir;
//k = t2.Days - t3.Days;//第二种判断方法
Console.WriteLine("您今年生日已过,明年生日距今还有"+ (k+j) +"天");//明年生日,还有多久
}
Console.WriteLine("您的生日是在" + bir.DayOfWeek + "," +"是生日当年的第"+ bir.DayOfYear +"天");
}
catch
{
Console.WriteLine("您输入的格式有误!");
}
Console.ReadLine();