// 獲取用戶的身份證號碼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);