当前位置: 首页 > news >正文

uniapp开发小程序—根据生日日期计算年龄 周岁

0、需求

  1. 在UniApp开发小程序中,将接口返回的出生日期转化为年龄;
  2. 判断接口返回的年龄是否是周岁

可以使用JavaScript的日期处理方法来实现。

一、第一种方式(示例代码):

//javascript
// 假设接口返回的年龄为生日的日期字符串,如 '1990-01-01'
const birthday = '1990-01-01';const now = new Date();  // 获取当前日期
const birthDate = new Date(birthday); // 将生日字符串转换为日期对象
const ageDiff = now.getFullYear() - birthDate.getFullYear(); // 计算年龄差值// 如果生日还未过,则年龄减1
if (now.getMonth() < birthDate.getMonth() || (now.getMonth() === birthDate.getMonth() && now.getDate() < birthDate.getDate())) {ageDiff--;
}
console.log(ageDiff); // 输出:31// 判断年龄是否是周岁
const isFullAge = ageDiff >= 1;
console.log(isFullAge); // 输出:true

二、第二种方式

<view class="age">{{getAge(info.birth)}}岁</view>methods: {//出生日期转化为年龄getAge(val){let currentYear = new Date().getFullYear() //当前的年份let calculationYear = new Date(val).getFullYear() //计算的年份const wholeTime = currentYear + val.substring(4) //周岁时间const calculationAge = currentYear - calculationYear //按照年份计算的年龄//判断是否过了生日if (new Date().getTime() > new Date(wholeTime).getTime()){return calculationAge}else {return calculationAge - 1}},
}

完成~

在这里插入图片描述

http://www.lryc.cn/news/211064.html

相关文章:

  • windows下基于vscode的ssh服务远程连接ubuntu服务器
  • OpenCV学习(二)——OpenCV中绘图功能
  • 业务架构、应用架构、技术架构、数据架构
  • 独创改进 | RT-DETR 引入 Asymptotic Hybrid Encoder | 渐进混合特征解码结构
  • SpringCloudAlibaba实战-nacos集群部署
  • Elasticsearch安装IK分词器
  • 『51单片机』 DS1302时钟
  • ubuntu部署个人网盘nextCloud使用docker-compose方式
  • 【ChatGPT 01】ChatGPT基础科普
  • 2317.操作后的最大异或和
  • Python爬虫-经典案例详解
  • 【信创】银河麒麟V10 安装postgis
  • OpenCV常用功能——灰度处理和图像二值化处理
  • excel巧用拼接函数CONCAT输出JSON、SQL字符串
  • Redis桌面管理工具:Redis Desktop Manager for Mac
  • 基于SSM的汽车维修管理系统
  • volatile 系列之如何解决可见性问题
  • 网络安全(黑客技术)—小白自学
  • MTK AEE_EXP调试方法及user版本打开方案
  • 京东平台数据分析:2023年9月京东扫地机器人行业品牌销售排行榜
  • pytorch 笔记:index_select
  • 面试算法43:在完全二叉树中添加节点
  • Python算法例3 检测2的幂次
  • 线扫相机DALSA--采集卡Base模式设置
  • Gitee 发行版
  • python面向对象
  • Go基础——数组、切片、集合
  • Error: no matching distribution found for tensorflow-cpu==2.6.*
  • nginx 进程模型
  • TypeScript - 枚举类型 -字符型枚举