使用js獲取身份證年齡的示例代碼
更新時(shí)間:2020年12月11日 16:06:36 作者:china丶MRH
這篇文章主要介紹了使用js獲取身份證年齡的示例代碼,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下
/** 根據(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)文章
JavaScript獲取網(wǎng)頁的寬高及如何兼容詳解
這篇文章主要給大家介紹了關(guān)于JavaScript獲取網(wǎng)頁的寬高及如何兼容的相關(guān)資料,主要介紹了三個(gè)方式,分別是window.innerWidth / window.innerHeight、document.documentElement.clientWidth以及混雜模式/怪異模式 下的寬高的獲取,需要的朋友可以參考下2021-05-05layui將table轉(zhuǎn)化表單顯示的方法(即table.render轉(zhuǎn)為表單展示)
今天小編就為大家分享一篇layui將table轉(zhuǎn)化表單顯示的方法(即table.render轉(zhuǎn)為表單展示),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09淺談JS封閉函數(shù)、閉包、內(nèi)置對(duì)象
下面小編就為大家?guī)硪黄獪\談JS封閉函數(shù)、閉包、內(nèi)置對(duì)象。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07如何利用Web Speech API之speechSynthesis實(shí)現(xiàn)文字轉(zhuǎn)語音功能
Web Speech API使你能夠?qū)⒄Z音數(shù)據(jù)合并到Web應(yīng)用程序中,SpeechSynthesisUtterance是HTML5中新增的API,用于將指定文字合成為對(duì)應(yīng)的語音,這篇文章主要介紹了利用Web Speech API之speechSynthesis實(shí)現(xiàn)文字轉(zhuǎn)語音功能,需要的朋友可以參考下2024-06-06JavaScript對(duì)象的創(chuàng)建模式與繼承模式示例講解
繼承機(jī)制是面向?qū)ο蟪绦蛟O(shè)計(jì)使代碼可以復(fù)用的最重要的手段,它允許程序員在保持原有的特性基礎(chǔ)上進(jìn)行擴(kuò)展,增加功能,這樣產(chǎn)生新的類,稱作是派生類。繼承呈現(xiàn)了面向?qū)ο蟪绦蛟O(shè)計(jì)的層析結(jié)構(gòu),體現(xiàn)了由簡(jiǎn)單到復(fù)雜的認(rèn)知過程。繼承是類設(shè)計(jì)層次的復(fù)用2022-12-12網(wǎng)頁中右鍵功能的實(shí)現(xiàn)方法之contextMenu的使用
本文介紹一種網(wǎng)頁中實(shí)現(xiàn)右鍵功能的方案–contextMenu,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02