根据身份证号
计算年龄
///
<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();
}