使用js獲取身份證年齡的示例代碼
/**
根據(jù)身份證號(hào)碼判斷性別
15位身份證號(hào)碼:第7、8位為出生年份(兩位數(shù)),第9、10位為出生月份,第11、12位代表出生日
18位身份證號(hào)碼:第7、8、9、10位為出生年份(四位數(shù)),第11、第12位為出生月份,
第13、14位代表出生日期,第17位代表性別,奇數(shù)為男,偶數(shù)為女。
*/
//根據(jù)身份證號(hào)獲取年齡
GetAge(identityCard) {
let len = (identityCard + "").length;
let strBirthday = "";
if (len == 18) {
//處理18位的身份證號(hào)碼從號(hào)碼中得到生日和性別代碼
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);
}
}
//時(shí)間字符串里,必須是“/”
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 獲取身份證年齡的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
js中通過getElementsByName訪問name集合對(duì)象的方法
下面小編就為大家?guī)硪黄猨s中通過getElementsByName訪問name集合對(duì)象的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
JS實(shí)現(xiàn)Fisheye效果動(dòng)感放大菜單代碼
這篇文章主要介紹了JS實(shí)現(xiàn)Fisheye效果動(dòng)感放大菜單代碼,涉及JavaScript事假監(jiān)聽機(jī)制及定時(shí)函數(shù)等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
firefox下input type="file"的size是多大
firefox對(duì)type="file" 的input的width定義目前是不支持的,但是FF支持size屬性,可以給size設(shè)置一個(gè)值,來控制上傳框的大小2011-10-10
JS小功能(列表頁面隔行變色)簡單實(shí)現(xiàn)
這篇文章主要介紹了JS列表頁面隔行變色簡單實(shí)現(xiàn),有需要的朋友可以參考一下2013-11-11
使用JavaScript實(shí)現(xiàn)alert的實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了js實(shí)現(xiàn)alert的方法,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07
JS監(jiān)控關(guān)閉瀏覽器操作的實(shí)例詳解
這篇文章主要介紹了JS監(jiān)控關(guān)閉瀏覽器操作的實(shí)例詳解的相關(guān)資料,希望通過本大家能幫助到大家,需要的朋友可以參考下2017-09-09
JavaScript talbe表中指定位置插入一行的實(shí)現(xiàn)代碼 腳本之家修正版
用js實(shí)現(xiàn)的在table中指定的位置插入一行,先點(diǎn)一下表中你想插入的位置,點(diǎn)擊即可。2009-06-06

