当前位置:网站首页>JS to obtain age through ID card
JS to obtain age through ID card
2022-07-23 14:13:00 【Kai Xiaomo】
function getAge(identityCard) {
var len = (identityCard + "").length;
if (len == 0) {
return '';
} else {
if ((len != 15) && (len != 18)) // The ID card number can only be 15 Bit or 18 Others are illegal
{
return '';
}
}
var strBirthday = "";
if (len == 18) // Handle 18 The ID number of bits gets the birthday and gender code from the number
{
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);
}
// In the time string , Must be “/”
var birthDate = new Date(strBirthday);
var nowDateTime = new Date();
var age = nowDateTime.getFullYear() - birthDate.getFullYear();
// Think about the month 、 The element of heaven ;.getMonth() What we get is from 0 At the beginning , Here's a comparison , No need to add 1
if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
边栏推荐
猜你喜欢

What's the difference between core i9 12950hx and i9 12900hx

英特尔赛扬7300性能怎么样?相当于什么水平级别

Principle of container network

数据库连接池 & DBUtils

Head products generated 2.5 billion yuan, and SLG track was also targeted by black products

Rtx3080ti and rtx3080 gap 3080 and 3080ti parameter comparison

rtx3080ti和3090差距 rtx3080ti和3090哪个性价比高

What level is the notebook core i5 1135g7 equivalent to? How about i5 1135g7 performance

MySQL enables scheduled task execution

How can Creo 9.0 quickly modify CAD coordinate system?
随机推荐
rip的详细介绍
第十天笔记
How many processors is Tianji 720 equivalent to Xiaolong? How about Tianji 720 equivalent to Xiaolong
Differences between Xiaomi 12s pro and Xiaomi 12pro Tianji version configuration comparison between the two
Notes du jour 5
Canvas eraser function
BGP federal experiment
Excel essay record
Notes on the fourth day
Notes on the seventh day
Rtx3080 is equivalent to GTX. What kind of graphics card is rtx3080? What level is rtx3080
接口interface
fastadmin更改默认表格按钮的弹窗大小
Detailed introduction of RIP
Static comprehensive experiment (HCIA)
达人评测酷睿i7 12850hx和i7 12700h选哪个
Creo 9.0 中几何对象的选取方法
Creo 9.0 如何快速修改CAD坐标系?
Review of HCIA
How many processors is Tianji 1100 equivalent to snapdragon? How about Tianji 1100 equivalent to snapdragon