vue根据身份证获取年龄/生日/性别,根据年龄获取生日,根据生日获取年龄

文章介绍了如何使用JavaScript实现根据身份证获取年龄、生日和性别的功能,以及如何根据年龄和生日计算对应的日期。函数包括handleBlur处理输入框模糊事件,calculateBirthdate计算出生日期,calculateAge计算年龄。
摘要由CSDN通过智能技术生成

首先根据身份证获取年龄/生日/性别

 handleBlur(data) {
      let idCard = data.target.value;
      let sex = null;
      let birth = null;
      let myDate = new Date();
      let month = myDate.getMonth() + 1;
      let day = myDate.getDate();
      let age = 0;

      if (idCard.length === 18) {
        age = myDate.getFullYear() - idCard.substring(6, 10) - 1;
        sex = idCard.substring(16, 17);
        birth =
          idCard.substring(6, 10) +
          "-" +
          idCard.substring(10, 12) +
          "-" +
          idCard.substring(12, 14);
        if (
          idCard.substring(10, 12) < month ||
          (idCard.substring(10, 12) === month && idCard.substring(12, 14) <= day)
        ) {
          age++;
        }
      } else if (idCard.length === 15) {
        age = myDate.getFullYear() - idCard.substring(6, 8) - 1901;
        sex = idCard.substring(13, 14);
        birth =
          "19" +
          idCard.substring(6, 8) +
          "-" +
          idCard.substring(8, 10) +
          "-" +
          idCard.substring(10, 12);
        if (
          idCard.substring(8, 10) < month ||
          (idCard.substring(8, 10) === month && idCard.substring(10, 12) <= day)
        )
          age++;
      }

      if (sex % 2 === 0) {
        sex = 2;
      } else {
        sex = 1;
      }

      this.formData.customerAge = age;
      this.formData.gender = sex;
      this.formData.birthday = birth;
    },

根据年龄获取生日,年份根据年龄算,月日取当天日期

function calculateBirthdate(age) {
  // 获取当前日期
  const currentDate = new Date();

  // 获取当前年份
  const currentYear = currentDate.getFullYear();

  // 计算出生年份
  const birthYear = currentYear - age;

  // 获取当前月份和日期
  const currentMonth = currentDate.getMonth() + 1; // 月份是从0开始的,所以要加1
  const currentDay = currentDate.getDate();

  // 构建出生日期的字符串
  const birthdate = `${birthYear}-${currentMonth}-${currentDay}`;

  return birthdate;
}

// 示例用法
const age = 25; // 用你实际的年龄替换
const birthdate = calculateBirthdate(age);
console.log(birthdate);

根据生日获取年龄

function calculateAge(birthdate) {
  // 将出生日期字符串转换为日期对象
  const birthDateObj = new Date(birthdate);

  // 获取当前日期
  const currentDate = new Date();

  // 计算年龄
  let age = currentDate.getFullYear() - birthDateObj.getFullYear();

  // 考虑生日还未到的情况
  const currentMonth = currentDate.getMonth() + 1; // 月份是从0开始的,所以要加1
  const birthMonth = birthDateObj.getMonth() + 1;

  if (currentMonth < birthMonth || (currentMonth === birthMonth && currentDate.getDate() < birthDateObj.getDate())) {
    age--;
  }

  return age;
}

// 示例用法
const birthdate = '1990-05-15'; // 用实际的出生日期替换
const age = calculateAge(birthdate);
console.log(age);

Vue中根据身份证获取性别出生日期和省份信息,通常是通过解析身份证号码来实现的。中国大陆的居民身份证号码由18位数字组成,其中第7到14位为出生年月日,第17位代表性别(奇数为男性,偶数为女性),前6位为行政区划代码,可以用来判断省份。 以下是一个简单的Vue组件示例,演示如何在Vue实现这一功能: ```javascript <template> <div> <input type="text" v-model="idNumber" placeholder="请输入身份证号"> <button @click="getIdInfo">获取信息</button> <div v-if="idInfo"> <p>性别:{{ idInfo.gender }}</p> <p>出生日期:{{ idInfo.birth }}</p> <p>省份:{{ idInfo.province }}</p> </div> </div> </template> <script> export default { data() { return { idNumber: '', idInfo: null, }; }, methods: { getIdInfo() { if (!this.idNumber) { alert('请输入身份证号码!'); return; } if (this.idNumber.length === 18) { const province = this.getProvince(this.idNumber.substring(0, 6)); const birth = this.idNumber.substring(6, 14); const gender = this.getGender(this.idNumber.substring(16, 17)); this.idInfo = { gender: gender, birth: birth, province: province, }; } else { alert('请输入正确的身份证号码!'); } }, getProvince(code) { // 这里可以通过一个行政区划代码的映射表来获取省份 // 实际开发中可能需要从服务器获取完整的行政区划数据表 const provinceMap = { // 示例映射 '110000': '北京市', '310000': '上海市', // 其他省市区代码和省份的映射... }; return provinceMap[code] || '未知省份'; }, getGender(genderCode) { // 根据身份证号码的第17位判断性别 return genderCode % 2 === 0 ? '女' : '男'; }, }, }; </script> ``` 这段代码提供了一个输入框让用户输入身份证号码,并通过点击按钮来触发获取身份证信息的方法。`getIdInfo` 方法会根据输入的身份证号码,提取出生日期性别信息,同时调用 `getProvince` 方法来获取省份信息。在实际应用中,可能需要调用后端API来获取完整的行政区划数据,这里仅为演示用了一段简化的映射数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值