获取身份证中的性别,代码如下:
public static bool GetGenderByIdCard(string idCard)
{
if (string.IsNullOrWhiteSpace(idCard))
{
return false;
}
return Convert.ToBoolean(int.Parse(idCard.Substring(16, 1)) % 2);
}
获取身份证中的年龄,代码如下:
public static int GetAgeByIdCard(string idCard)
{
int age = 0;
if (!string.IsNullOrWhiteSpace(idCard))
{
var subStr = string.Empty;
if (idCard.Length == 18)
{
subStr = idCard.Substring(6, 8).Insert(4, "-").Insert(7, "-");
}
else if (idCard.Length == 15)
{
subStr = ("19" + idCard.Substring(6, 6)).Insert(4, "-").Insert(7, "-");
}
TimeSpan ts = DateTime.Now.Subtract(Convert.ToDateTime(subStr));
age = ts.Days / 365;
}
return age;
}