#region 根據出生日期得到年龄
/// <summary>
/// 根據出生日期得到年龄/// </summary>
/// <param>輸入出生日期</param>
/// <returns>傳回年龄</returns>
public string getAge(string strBirthday)
{
string _age = ""; //年龄
int _differYear; //是否滿年
//判断傳入的出生日期是否为空
if (!string.IsNullOrEmpty(strBirthday))
{
try
{
//將傳入的出生日期轉换為日期格式
DateTime _Birthday = Convert.ToDateTime(strBirthday);
//精确判断(精确到天数)
if (System.DateTime.Now.Month >= _Birthday.Month)
{
if (System.DateTime.Now.Day >= _Birthday.Day)
{
_differYear = 0;
}
else
{
_differYear = 1;
}
}
else
{
_differYear = 1;
}
//结果
_age = (DateTime.Now.Year - _Birthday.Year - _differYear).ToString();
}
catch
{
return "Error";
}
}
else
{
_age = "参数错误";
}
return _age;
}
#endregion
留言列表