JS根据身份证号码算年龄
生活随笔
收集整理的這篇文章主要介紹了
JS根据身份证号码算年龄
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
如果把身份證號(hào)碼傳到頁(yè)面上,在前端頁(yè)面獲取年齡就需要用到JS腳本了:
function GetAge(identityCard) {
var len = (identityCard + "").length;
if (len == 0) {
return 0;
} else {
if ((len != 15) && (len != 18))//身份證號(hào)碼只能為15位或18位其它不合法
{
return 0;
}
}
var 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) {
strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
}
//時(shí)間字符串里,必須是“/”
var birthDate = new Date(strBirthday);
var nowDateTime = new Date();
var 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;
}
作者:Eric Li
出處:http://www.cnblogs.com/ericli-ericli/
除轉(zhuǎn)載文章外,隨筆版權(quán)歸作者和博客園所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)標(biāo)明出處。
如果您覺(jué)得本篇博文對(duì)您有所收獲,覺(jué)得作者還算用心,請(qǐng)點(diǎn)擊右下角的 [推薦],謝謝!
總結(jié)
以上是生活随笔為你收集整理的JS根据身份证号码算年龄的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android开发之fragment之r
- 下一篇: 酷云EYE Pro:引领客厅进入大数据时