其他分享
首页 > 其他分享> > //根据身份证号自动生成性别、出生日期和年龄

//根据身份证号自动生成性别、出生日期和年龄

作者:互联网

//根据身份证号自动生成性别、出生日期和年龄
    inputChange() {
      const reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
      if (reg.test(this.dataList.identityCardNo)) { // 身份证号码是否合法
        var org_birthday = this.dataList.identityCardNo.substring(6, 14);
        var org_gender = this.dataList.identityCardNo.substring(16, 17);
        var sex = org_gender % 2 == 1 ? "男" : "女";      
        var birthday = org_birthday.substring(0, 4) + "-" + org_birthday.substring(4, 6) + "-" + org_birthday.substring(6, 8);
        var birthdays = new Date(birthday.replace(/-/g, "/"));
        let d = new Date();
        let age = d.getFullYear() - birthdays.getFullYear() - (d.getMonth() < birthdays.getMonth() || (d.getMonth() == birthdays.getMonth() && d.getDate() < birthdays.getDate()) ? 1 : 0);
        
        // console.log('生日转换时间', birthdays)
        // console.log('年龄', age)

        // 赋值给表格
        this.dataList.sex = sex
        this.dataList.birthday = birthday
        this.dataList.age = age
      } else {
        this.dataList.sex = "未填写"
        return false
      }
    },

作者:泡芙123
链接:https://juejin.cn/post/6988426072786206733
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

标签:dataList,birthdays,身份证号,substring,birthday,org,var,出生日期,性别
来源: https://blog.csdn.net/YhWyl527/article/details/122212226