其他分享
首页 > 其他分享> > 身份证计算器

身份证计算器

作者:互联网

class IDCardComputer {
    constructor(idCard) {
      if (!idCard || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(idCard)) {
        throw new Error('身份证号格式错误')
      }
      this.idCard = idCard;
      this.data={}
    }
    //获取出生日期
    getBirthday() {
      let birthday = this.idCard.substring(6, 10) + "-" + this.idCard.substring(10, 12) + "-" + this.idCard.substring(12, 14)
      this.data.birthday=birthday
      return this
    }
    //获取性别
    getSex() {
      if (parseInt(this.idCard.substr(16, 1)) % 2 == 1) {
        this.data.sex={ name: "男", value: '1' }
      } else {
        this.data.sex={ name: "女", value: '2' }
      }
      return this
    }
    //获取年龄
    getAge() {
      var ageDate = new Date()
      var month = ageDate.getMonth() + 1
      var day = ageDate.getDate()
      var age = ageDate.getFullYear() - this.idCard.substring(6, 10) - 1
      if (this.idCard.substring(10, 12) < month || this.idCard.substring(10, 12) == month && this.idCard.substring(12, 14) <= day) {
        age++
      }
      if (age <= 0) {
        age = 1
      }
      this.data.age=age
      return this
    }

    //全部计算
    All(){
     return this.getAge().getBirthday().getSex().data
    }

  }

标签:10,12,age,idCard,substring,身份证,计算器,data
来源: https://blog.csdn.net/qq_44649893/article/details/120634494