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

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

var strBirthday = "";
if (len == 18)//18位身份证号
{
strBirthday = 身份证号.substr(6, 4) + "/" + 身份证号.substr(10, 2) + "/" + 身份证号.substr(12, 2);
}
if (len == 15)//15位身份证号

{
strBirthday = "19" + 身份证号.substr(6, 2) + "/" + 身份证号.substr(8, 2) + "/" + 身份证号.substr(10, 2);
}

//时间字符串里,必须是“/”隔开
var birthDate = new Date(strBirthday);
var nowDateTime = new Date();
var age = nowDateTime.getFullYear() - birthDate.getFullYear();
//月、天的因素;.getMonth()获取的是从0开始
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);

 
小白的日常
原文地址:https://www.cnblogs.com/Stranger-WY/p/14015156.html