// 获取用户的身份证号码let identityCard = this.idNum.replace(/\s+/g, "");//判断长度let len = identityCard.length;//设置新的变量var strBirthday = "";//根据长度获取年月日if (len == 18) {strBirthday =identityCard.substr(6, 4) +"/" +identityCard.substr(10, 2) +"/" +identityCard.substr(12, 2);}if (len == 15) {strBirthday ="19" +identityCard.substr(6, 2) +"/" +identityCard.substr(8, 2) +"/" +identityCard.substr(10, 2);}//格式化用户的年月日信息let birthDate = new Date(strBirthday);//创建新的年月日信息let nowDateTime = new Date();//新的年份 - 用户年份var age = nowDateTime.getFullYear() - birthDate.getFullYear();//通过月份最终获取用户年龄if (nowDateTime.getMonth() < birthDate.getMonth() ||(nowDateTime.getMonth() == birthDate.getMonth() &&nowDateTime.getDate() < birthDate.getDate())) {age--;}//查看年龄console.log(age);