计算年龄
private void age(string date)
{
string rvalue = string.empty;
string nowdate = datetime.now.tostring("yyyymmdd");
datetime brithdate = datetime.parse(date.insert(6, "-").insert(4, "-"));
if (datetime.now.year == brithdate.year) //年份相等
{
#region
if (datetime.now.month == brithdate.month) //月份相等
{
rvalue = (datetime.now.day - brithdate.day).tostring() + "天";
}
else //月份不相等
{
if (datetime.now.day >= brithdate.day)
{
rvalue = (datetime.now.month - brithdate.month).tostring() + "月";
}
else
{
rvalue = (datetime.now.month - brithdate.month - 1).tostring() + "月";
}
}
#endregion
}
else //年份不相等
{
#region
if (datetime.now.month > brithdate.month)
{
if (datetime.now.day >= brithdate.day)
rvalue = (datetime.now.year - brithdate.year).tostring() + "年"+(datetime.now.month-brithdate.month).tostring()+"月";
else
rvalue = (datetime.now.year - brithdate.year).tostring() + "年" + (datetime.now.month - brithdate.month-1).tostring() + "月";
}
else if (datetime.now.month < brithdate.month)
{
if (datetime.now.year - brithdate.year - 1 == 0)
{ //eg:出生日期:20111020,当前日期:20120820
if (datetime.now.day >= brithdate.day)
{
rvalue = (12 - brithdate.month + datetime.now.month ).tostring() + "月";
}
else
{
rvalue = (12 - brithdate.month + datetime.now.month - 1).tostring() + "月";
}
}
else
{
rvalue = (datetime.now.year - brithdate.year - 1).tostring() + "年" + (12- brithdate.month +datetime.now.month - 1).tostring() + "月";
}
}
else //年份不一样,月份相等
{
if (datetime.now.day >= brithdate.day)
{
rvalue = (datetime.now.year - brithdate.year).tostring() + "年";
}
else
{
if (datetime.now.year - brithdate.year - 1 == 0)
{ //eg:出生日期:20111020,当前日期:20120820
if (datetime.now.day >= brithdate.day)
{
rvalue = (datetime.now.year - brithdate.year).tostring() + "年";
}
else
{
rvalue = (12 - brithdate.month + datetime.now.month - 1).tostring() + "月";
}
}
else
{
rvalue = (datetime.now.year - brithdate.year - 1).tostring() + "年";
}
}
}
#endregion
}
messagebox.show(rvalue.tostring());
}