美文网首页
正则效验从身份证中提取性别和生日

正则效验从身份证中提取性别和生日

作者: 地主家也没余粮叻 | 来源:发表于2019-03-12 10:39 被阅读0次
// 从身份证中提取生日
    getBirthdayFromIdCard(idCard) {  
      let birthday = "";  
      if(idCard != null && idCard != ""){  
        if(idCard.length == 15){  
            birthday = "19"+idCard.substr(6,6);  
        } else if(idCard.length == 18){  
            birthday = idCard.substr(6,8);  
        }  
        birthday = birthday.replace(/(.{4})(.{2})/,"$1-$2-");  
      }  
      return birthday;  
    },
    // 从身份证中提取性别
    getBirthdayFromSex(sex) { 
      let gender = ""; 
      if(sex != null && sex != ""){ 
      if(sex.length == 15){ 
        gender = "19"+sex.substr(6,6); 
      } else if(sex.length == 18){ 
        gender = parseInt(sex.slice(14,17));
        if(gender % 2 === 1) { // 男
          gender = 1
          } else { // 女
          gender = 2
          }
        } 
      } 
      return gender; 
    }
// 调用
let birthday =this.getBirthdayFromIdCard('身份证号')
let sex=this.getBirthdayFromSex('身份证号')

相关文章

网友评论

      本文标题:正则效验从身份证中提取性别和生日

      本文链接:https://www.haomeiwen.com/subject/thkgpqtx.html