JS中根据身份证号获取年龄、出生日期

//获取年龄
function GetAge(身份证号) {

var Birthday = "";
if (len == 18)//处理18位的身份证号码从号码中得到生日和性别代码
{
Birthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
}
if (len == 15) {
Birthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.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;
}

//获取出生日期

//18位身份证号

var birthday=身份证号.substr(6, 4) + "-" +身份证号.substr(10, 2) + "-" + 身份证号.substr(12, 2);

//15为身份证号

var birthday = "19" + 身份证号.substr(6, 2) + "/" + 身份证号.substr(8, 2) + "/" + 身份证号.substr(10, 2);

 
上一篇:js 利用Date对象得到某个月份


下一篇:习题11-1 输出月份英文名 (15分)