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

//根据身份证号自动生成性别、出生日期和年龄
    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
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值