JS版身份证获取性别、出生日期及年龄
数组结构为:
arr:{sex:'',birthday:'',age:''}
function getCarInfo(UUserCard) {
//获取输入身份证号码
var arr = {};
if(UUserCard != null && UUserCard != ''){
//获取出生日期
var birthday = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" +
UUserCard.substring(12, 14);
arr.birthday = birthday;
//获取性别
if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
arr.sex = '男'
} else {
arr.sex = '女'
}
//获取年龄
var myDate = new Date();
var month = myDate.getMonth() + 1;
var day = myDate.getDate();
var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) ==
month && UUserCard.substring(12, 14) <= day) {
age++;
}
arr.age = age;
//$("#age").val(age);
//年龄 age
}
return arr
}
getCarInfo('210727198010203310')