根據身份證號計算年齡
阿新 • • 發佈:2019-02-13
根據身份證號計算年齡
///
<summary>
///
計算年齡
///
</summary>
///
<param name="str">18位身份證號碼</param>
///
<returns></returns>
public
string
CalculateAgeCorrect(string
str)
{
string
Sub_str = str.Substring(6, 8).Insert(4,
"-").Insert(7,
"-");
//提取出生年月日,"1976-08-09"
DateTime
birthDate =
Convert.ToDateTime(Sub_str);
DateTime
now =
DateTime.Now;
int
age = now.Year - birthDate.Year;
if
(now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day)) age--;
return
age.ToString();
}