function getIdcardAge(idcard) {
var len = (idcard + "").length;
if (len == 0) {
return 0;
} else {
if ((len != 15) && (len != 18)) {
return 0;
}
}
var strBirthday = "";
if (len == 18) {
strBirthday = idcard.substr(6, 4) + "/" + idcard.substr(10, 2) + "/" + idcard.substr(12, 2);
}
if (len == 15) {
strBirthday = "19" + idcard.substr(6, 2) + "/" + idcard.substr(8, 2) + "/" + idcard.substr(10,
2);
}
//时间字符串里,必须是“/”
var birthDate = new Date(strBirthday);
var nowDateTime = new Date();
var age = nowDateTime.getFullYear() - birthDate.getFullYear();
//再考虑月、天的因素;.getMonth()获取的是从0开始的,这里进行比较,不需要加1
if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime
.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
根据身份证号计算年龄
最新推荐文章于 2025-05-13 13:33:12 发布