首页 > 技术文章 > vue 根据身份证计算出出生日期和判断性别

majiayin 2021-05-21 16:48 原文

//获取生日和性别
    getBirth(idCard) {
      
      var birthday = "";

      if(idCard != null && idCard != ""){
        if(idCard.length == 15){
          birthday = "19"+idCard.slice(6,12);
        } else if(idCard.length == 18){
          birthday = idCard.slice(6,14);
        }   
        this.studentForm.birthday = birthday.replace(/(.{4})(.{2})/,"$1-$2-");
        //通过正则表达式来指定输出格式为:1990-01-01
      }

      if (parseInt(idCard.slice(-2, -1)) % 2 == 1) {
        this.studentForm.sex = '0';
      }
      else {
        this.studentForm.sex = '1';
      }

    },

推荐阅读