使用js獲取身份證年齡的示例代碼
/** 根據(jù)身份證號碼判斷性別 15位身份證號碼:第7、8位為出生年份(兩位數(shù)),第9、10位為出生月份,第11、12位代表出生日 18位身份證號碼:第7、8、9、10位為出生年份(四位數(shù)),第11、第12位為出生月份, 第13、14位代表出生日期,第17位代表性別,奇數(shù)為男,偶數(shù)為女。 */ //根據(jù)身份證號獲取年齡 GetAge(identityCard) { let len = (identityCard + '').length; let strBirthday = ''; if (len == 18) { //處理18位的身份證號碼從號碼中得到生日和性別代碼 strBirthday = identityCard.substr(6, 4) + '/' + identityCard.substr(10, 2) + '/' + identityCard.substr(12, 2); } if (len == 15) { let birthdayValue = ''; birthdayValue = identityCard.charAt(6) + identityCard.charAt(7); if (parseInt(birthdayValue) < 10) { strBirthday = '20' + identityCard.substr(6, 2) + '/' + identityCard.substr(8, 2) + '/' + identityCard.substr(10, 2); } else { strBirthday = '19' + identityCard.substr(6, 2) + '/' + identityCard.substr(8, 2) + '/' + identityCard.substr(10, 2); } } //時間字符串里,必須是“/” let birthDate = new Date(strBirthday); let nowDateTime = new Date(); let age = nowDateTime.getFullYear() - birthDate.getFullYear(); //再考慮月、天的因素;.getMonth()獲取的是從0開始的,這里進(jìn)行比較,不需要加1 if ( nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate()) ) { age--; } return age; }
以上就是使用js獲取身份證年齡的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于js 獲取身份證年齡的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. PHP設(shè)計模式中工廠模式深入詳解2. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明3. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)4. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析5. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測可用)6. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法7. ASP.NET MVC遍歷驗證ModelState的錯誤信息8. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. CSS hack用法案例詳解
