js通过身份证获取年龄
生活随笔
收集整理的這篇文章主要介紹了
js通过身份证获取年龄
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// 獲取用戶的身份證號碼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);
總結
以上是生活随笔為你收集整理的js通过身份证获取年龄的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js动态验证码获取
- 下一篇: this.$router 的三种跳转页面