javascript js根据身份证号识别性别、年龄、生日年月

idCard() {
      // 获取输入身份证号码
      var cardNum = "";

      //获取出生日期
      cardNum.substring(6, 10) +
        "-" +
        cardNum.substring(10, 12) +
        "-" +
        cardNum.substring(12, 14);

      // 获取性别
      if (parseInt(cardNum.substr(16, 1)) % 2 == 1) {
        alert("男");
      } else {
        alert("女");
      }

      //获取年龄

      var myDate = new Date();

      var month = myDate.getMonth() + 1;

      var day = myDate.getDate();

      var age = myDate.getFullYear() - cardNum.substring(6, 10) - 1;

      if (
        cardNum.substring(10, 12) < month ||
        (cardNum.substring(10, 12) == month && cardNum.substring(12, 14) <= day)
      ) {
        age++;
      }

      //年龄 age

      alert(age);
    },

你可能感兴趣的:(javascript js根据身份证号识别性别、年龄、生日年月)