function IdCard(IdCard, type) {
//获取出生日期
if (type === 1) {
let birthday = IdCard.substring(6, 10) + "-" + IdCard.substring(10, 12) + "-" + IdCard.substring(12, 14)
return birthday
}
//获取性别
if (type === 2) {
if (parseInt(IdCard.substr(16, 1)) % 2 === 1) {
return "男"
} else {
return "女"
}
}
//获取年龄
if (type === 3) {
var ageDate = new Date()
var month = ageDate.getMonth() + 1
var day = ageDate.getDate()
var 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 age
}
}
js身份证识别出生日期、年龄、性别
最新推荐文章于 2024-09-19 14:02:34 发布