当type=1时获取出生日期,type=2时获取性别,type=3时获取年龄
javascript
/*
* 当type=1时获取出生日期,type=2时获取性别,type=3时获取年龄
* */
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 1 //男
} else {
return 2 //女
}
}
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
}
}
给大家推荐一个实用面试题库
1、前端面试题库 (面试必备) 推荐:★★★★★
地址:前端面试题库