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

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

复制代码
 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);
相关推荐
中微子几秒前
React Props 传值规范详解
前端·react.js
namehu1 分钟前
Taro 小程序 Video 组件 referrer-policy="origin" 属性失效排查记
前端·taro
卸任5 分钟前
性能优化大作战:React.memo 在可编辑列表中的奇效
前端·javascript·react.js
WildBlue14 分钟前
阮一峰闭包:JavaScript最优雅的"背包"魔法!✨
前端·javascript
LaoZhangAI17 分钟前
OpenAI API 账号分层完全指南:2025年最新Tier系统、速率限制与升级攻略
前端·后端
LeeAt17 分钟前
React Hooks 编程:useState和useEffect的详解
前端·react.js
Dream耀17 分钟前
React Hooks 指南:useState 与 useEffect 的用法与技巧
前端·javascript·react.js
zhanshuo20 分钟前
i18next + 原生JS 双引擎:打造前端多语言系统最佳实践
前端·javascript·css
小飞悟21 分钟前
React Hooks 编程:深入理解 useEffect 的执行机制与清理副作用
前端·javascript
庖丁解牛23 分钟前
3. Babylonjs 中获取相机方向相关
前端·webgl·游戏开发