根据身份证自动获取年龄/出生日期
<el-form-item label="证件号" prop="idCard"> <el-input v-model="form.idCard" size="small" :disabled="isDisabled" style="width:90%;" @blur="getName(form.idCard)" ></el-input> </el-form-item> methods: { getName(iden) { let val = iden.length; let sex = null; let birth = null; let myDate = new Date(); let month = myDate.getMonth() + 1; let day = myDate.getDate(); let age = 0;
//判断身份证位数为18位时去截取时间日期 if (val === 18) { age = myDate.getFullYear() - iden.substring(6, 10) - 1; sex = iden.substring(16, 17); birth = iden.substring(6, 10) + "-" + iden.substring(10, 12) + "-" + iden.substring(12, 14); if ( iden.substring(10, 12) < month || (iden.substring(10, 12) == month && iden.substring(12, 14) <= day) ) age++; }
//判断身份证位数为15位时去截取时间日期 if (val === 15) { age = myDate.getFullYear() - iden.substring(6, 8) - 1901; sex = iden.substring(13, 14); birth = "19" + iden.substring(6, 8) + "-" + iden.substring(8, 10) + "-" + iden.substring(10, 12); if ( iden.substring(8, 10) < month || (iden.substring(8, 10) == month && iden.substring(10, 12) <= day) ) age++; } this.form.age = age; this.form.dob = birth; // this.getDataInfo(); } }