const getInfoByIdcard = function (IdCardNO){
let info = {
age: '',
gender: '',
genderCn: '',
status:'201',
message: '',
}
let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if (reg.test(IdCardNO) === false)
{
info.status = "201"
info.message = "身份证号输入不合法:"+IdCardNO
}
let date = new Date();
let yearfull = date.getFullYear();
if (IdCardNO.length == 18)
{
let birthyear = IdCardNO.substring(6, 10);
let month = IdCardNO.substring(10, 12);
let day = IdCardNO.substring(12, 14);
let age = yearfull - Number(birthyear);
let genderValue = IdCardNO.substring(16, 17);
let gender = (Number(genderValue) % 2 == 0 ? "F" : "M");
let genderCn = (Number(genderValue) % 2 == 0 ? "女" : "男");
info.age = age.toString()
info.gender = gender
info.genderCn = genderCn
info.status = "200"
info.message = "身份证号解析完成"
}
if (IdCardNO.length == 15)
{
let birthDay = "";
birthDay = IdCardNO.substring(6, 12);
birthDay = "19" + birthDay;
birthDay = birthDay.substring(0, 4);
let age = yearfull - Number(birthDay);
let gender = parseInt(birthDay.substring(14, 1), 10) % 2 ? "F" : "M";
let genderCn = parseInt(birthDay.substring(14, 1), 10) % 2 ? "女" : "男";
info.age = age.toString()
info.gender = gender
info.genderCn = genderCn
info.status = "200"
info.message = "身份证号解析完成"
}
return info
}
console.log(
getInfoByIdcard("332001199812152811")
)
console.log(
getInfoByIdcard("36220119999999282x")
)
console.log(
getInfoByIdcard("382301199999992")
)
console