UNPKG

you-yuan-uni-ui

Version:

you-yuan-uni-ui 组件库

66 lines (63 loc) 1.47 kB
// 获取身份证号保存的信息 export default function IdCard(IdCard) { if (IdCard.length === 18) { const info = {}; //获取出生日期 info.birthday = IdCard.substring(6, 10) + '-' + IdCard.substring(10, 12) + '-' + IdCard.substring(12, 14); //获取性别 const getGender = () => { if (parseInt(IdCard.substr(16, 1)) % 2 === 1) { return { code: 1, name: '男' }; } else { return { code: 0, name: '女' }; } }; info.gender = getGender(); //获取年龄 const getAge = () => { var ageDate = new Date(); var month = ageDate.getMonth() + 1; var day = ageDate.getDate(); var age = ageDate.getFullYear() - IdCard.substring(6, 10) - 1; if ( IdCard.substring(10, 12) < month || (IdCard.substring(10, 12) === month && IdCard.substring(12, 14) <= day) ) { age++; } if (age <= 0) { age = 1; } return age; }; info.age = getAge(); return info; } } export function getAgeByBirthday(birthday) { if (!birthday) { return 0 } const ageDate = new Date(); const birthDay = new Date(birthday) const month = ageDate.getMonth() + 1; const day = ageDate.getDate(); let age = ageDate.getFullYear() - birthDay.getFullYear() - 1; if ( birthDay.getMonth() + 1 < month || (birthDay.getMonth() + 1 === month && birthDay.getDate() <= day) ) { age++; } if (age <= 0) { age = 1; } return age; }