计算年龄
根据出生日期,计算年龄(距当前时间),调用:Age(20121202); 方法如下:
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() + "月&quo
补充:Web开发 , ASP.Net ,