countAgeLimit(startTime){
// 获取当前日期
const currentDate = new Date();
// 将输入的日期转换为日期对象 startTime为yyyy-MM-dd格式日期
const startDate = new Date(startTime);
// 计算日期差值(以天为单位)
const diffTime = Math.abs(currentDate - startDate);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
// 计算年限
const years = Math.floor(diffDays / 365.25);
return years;
},
//举例 今天为2023-11-02
countAgeLimit("2023-02-03"); // 返回0,年限不满1年
countAgeLimit("2022-02-03"); // 返回1,年限不满2年,但满1年
本文介绍了一个JavaScript函数countAgeLimit,用于计算两个给定日期之间的年份差,以判断某个事件发生后是否已满一定年限。

被折叠的 条评论
为什么被折叠?



