/**
* 传入身份证号码,获取出生日期、性别和年龄
* @param IdCard 身份证号码
*/
function IdCard (IdCard) {
//获取出生日期
let birthday = IdCard.substring(6, 10) + "-" + IdCard.substring(10, 12) + "-" + IdCard.substring(12, 14)
//获取性别
let sex = parseInt(IdCard.substr(16, 1)) % 2 == 1?'男':'女'
//获取年龄
let ageDate = new Date()
let month = ageDate.getMonth() + 1
let day = ageDate.getDate()
let age = ageDate.getFullYear() - IdCard.substring(6, 10) - 1
if (IdCard.substring(10, 12) < month || IdCard.substring(10, 12) == month && IdCard.substring(12, 14) <= day) {
age++
}
if (age <= 0) {
age = 1
}
return {birthday :birthday ,sex:sex, age:age}
}
身份证解析年龄、性别、出生日期
最新推荐文章于 2022-06-14 16:57:38 发布