// 根据身份证获取信息
export const getCardInfo = (cardId: string | number, split: string = '-') => {
if (typeof cardId !== 'string' && typeof cardId !== 'number') return null
if (typeof cardId === 'number') cardId = `${cardId}`
let card: any = null;
// 获取身份证长度
const isNew = cardId.length === 18
// 根据长度计算日期
const birthData = isNew ? cardId.substr(6, 8) : `19${cardId.substr(6, 6)}`;
const birthYear = birthData.substr(0, 4);
const birthMounth = birthData.substr(4, 2);
const birthDay = birthData.substr(6, 2);
const birthDataSplit = `${birthYear}${split}${birthMounth}${split}${birthDay}`
// 18位的第17为代表性别奇数:男 偶数:女
// 15位的第15为代表性别奇数:男 偶数:女
const sexId = cardId.charAt(isNew ? 16 : 14);
const isBoy = Number(sexId) % 2;
const sex = isBoy ? '男' : '女'
card = {
birthData,
birthDataSplit,
birthYear,
birthMounth,
birthDay,
sex,
sexNum: sexId,
sexBolNum: isBoy ? 1 : 0,
}
return card
}
根据身份证信息获取出生日期
于 2022-05-13 15:18:36 首次发布