根据身份证获取性别生日等信息

(1)根据身份证获取性别

方法1:

private string sex(string identityCard)

{

string sex = "";

//处理18位的身份证号码从号码中得到生日和性别代码

if (identityCard.Length == 18)

{

sex = identityCard.Substring(14, 3);

}

//处理15位的身份证号码从号码中得到生日和性别代码

if (identityCard.Length == 15)

{

sex = identityCard.Substring(12, 3);

}

//性别代码为偶数是女性奇数为男性

if (Int_32(sex) % 2 == 0)

{

sex = "女";

}

else

{

sex = "男";

}

return sex;

}

方法2:

public static bool reg_idcard(string _card)

{

Regex reg = new Regex(@"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X|x)$");

if (reg.IsMatch(_card))

{

return true;

}

return false;

}

方法3:

public static string getSex(string _card)

{

int sex = int.Parse(_card.Substring(16, 1));

if (sex % 2 == 0)

{

return "女";

}

else

{

return "男";

}

}

(2) 根据身份证获取生日

private string birthday(string identityCard)

{

string birthday = "";

//处理18位的身份证号码从号码中得到生日和性别代码

if (identityCard.Length == 18)

{

birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-"+ identityCard.Substring(12, 2);

}

//处理15位的身份证号码从号码中得到生日和性别代码

if (identityCard.Length == 15)

{

birthday = "19" + identityCard.Substring(6, 2) + "-" + identityCard.Substring(8, 2)+ "-" + identityCard.Substring(10, 2);

}

return birthday;

}

猜你喜欢

转载自blog.csdn.net/JessicaQNL/article/details/82981245