解析身份证号,获取出生年月日和性别

const idCard = '111111111111111111111111'
if (idCard != null && idCard !== '' && (idCard.length === 15 || idCard.length === 18)) {
        let birthday = ''
        let sex = ''
        if (idCard.length === 15) {
          birthday = '19' + idCard.slice(6, 12)
        } else if (idCard.length === 18) {
          birthday = idCard.slice(6, 14)
        }
        //通过正则表达式来指定输出格式为:1990-01-01
        birthday  = birthday.replace(/(.{4})(.{2})/, '$1-$2-')
        if (parseInt(idCard.slice(-2, -1)) % 2 === 1) {
          sex = '男'
        } else {
          sex = '女'
        }
        
      }

标签: none

添加新评论