本文主要是介绍身份证号获取出生日期、年龄、性别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//身份证号获取出生日期
function GetBirthday(psidno) {
var birthdayno, birthdaytemp
if (psidno.length == 18) {
birthdayno = psidno.substring(6, 14)
} else if (psidno.length == 15) {
birthdaytemp = psidno.substring(6, 12)
birthdayno = "19" + birthdaytemp
} else {
return "";
}
var birthday = birthdayno.substring(0, 4) + "-"
+ birthdayno.substring(4, 6) + "-" + birthdayno.substring(6, 8)
return birthday;
}
//生日获取周岁
function ageFamater(value){
if(null!=value){
var returnAge;
var birthYear = value.substr(0,4);
var birthMonth = value.substr(5,2);
var birthDay = value.substr(8,2);
d = new Date();
var nowYear = d.getYear()+1900;
var nowMonth = d.getMonth() + 1;
var nowDay = d.getDate();
if(nowYear == birthYear){
returnAge = 0;//同年 则为0岁
}else{
var ageDiff = nowYear - birthYear ; //年之差
if(ageDiff > 0) {
if(nowMonth == birthMonth){
var dayDiff = nowDay - birthDay;//日之差
if(dayDiff < 0){
returnAge = ageDiff - 1;
}else{
returnAge = ageDiff ;
}
}else{
var monthDiff = nowMonth - birthMonth;//月之差
if(monthDiff < 0){
returnAge = ageDiff - 1;
}else{
returnAge = ageDiff ;
}
}
}else{
returnAge = "";//返回-1 表示出生日期输入错误 晚于今天
}
}
return returnAge;//返回周岁年龄
}
return "";
}
//身份证号获取性别
function Getsex(psidno){
var sexno,sex
if(psidno.length==18){
sexno=psidno.substring(16,17)
}else if(psidno.length==15){
sexno=psidno.substring(14,15)
}else{
alert("错误的身份证号码,请核对!")
return false
}
var tempid=sexno%2;
if(tempid==0){
sex='F'
}else{
sex='M'
}
return sex
}
这篇关于身份证号获取出生日期、年龄、性别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!