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);
相关推荐
包饭厅咸鱼8 分钟前
autojs----2025淘宝淘金币跳一跳自动化
java·javascript·自动化
OpenTiny社区18 分钟前
如何使用 TinyEditor 快速部署一个协同编辑器
前端·开源·编辑器·opentiny
IT_陈寒22 分钟前
震惊!我用JavaScript实现了Excel的这5个核心功能,同事直呼内行!
前端·人工智能·后端
前端伪大叔34 分钟前
freqtrade智能挂单策略,让你的资金利用率提升 50%+
前端·javascript·后端
江城开朗的豌豆35 分钟前
从“any”战士到类型高手:我的TypeScript进阶心得
前端·javascript·前端框架
麦麦大数据38 分钟前
F043 vue+flask天气预测可视化系统大数据+机器学习+管理端+爬虫+超酷界面+顶级可视化水平 【黑色版】
大数据·vue.js·flask·天气预测·气温预测·天气大数据·天气可视化
红尘散仙1 小时前
TRNovel王者归来:让小说阅读"声"临其境的终端神器
前端·rust·ui kit
知花实央l1 小时前
【Web应用安全】SQLmap实战DVWA SQL注入(从环境搭建到爆库,完整步骤+命令解读)
前端·经验分享·sql·学习·安全·1024程序员节
烛阴1 小时前
为你的Lua代码穿上盔甲:精通错误处理的艺术
前端·lua
深蓝电商API1 小时前
反反爬虫实战:手撕某知名网站Webpack加密的JavaScript
javascript·爬虫·webpack