/**
* 当天 周岁计算
* @param {生日} strBirthday
*/
export function getAge(strBirthday) {
let fullAge;
let strBirthdayArr = strBirthday.split("-");
let birthYear = strBirthdayArr[0];
let birthMonth = strBirthdayArr[1];
let birthDay = strBirthdayArr[2];
let d = new Date();
let nowYear = d.getFullYear();
let nowMonth = d.getMonth() + 1;
let nowDay = d.getDate();
if (nowYear == birthYear) {
fullAge = 0; //同年 则为0岁
} else {
let ageDiff = nowYear - birthYear; //年之差
if (ageDiff > 0) {
if (nowMonth == birthMonth) {
let dayDiff = nowDay - birthDay; //日之差
if (dayDiff < 0) {
fullAge = ageDiff - 1;
} else {
fullAge = ageDiff;
}
} else {
let monthDiff = nowMonth - birthMonth; //月之差
if (monthDiff < 0) {
fullAge = ageDiff - 1;
} else {
fullAge = ageDiff;
}
}
} else {
fullAge = -1; //返回-1 表示出生日期输入错误 晚于今天
}
}
return fullAge; //返回周岁年龄
}
/**
* 次日周岁计算
*/
// next day
export function get_next_day_age(birthday){
let brithMS = parseInt((new Date(birthday.split('-').join('/'))).getTime());
return getAge(date_format(brithMS - 24*60*60, "yyyy-MM-dd"));
}
/ / 和周岁计算函数方法一样 区别在于 周岁 计算 按当天时间 此方法为自定义事件
/**
* 获取两时间段相差 精确到日 返回 年 <number>
* @param {*} startTime
* @param {*} endTime
*/
export function getYearSub(startTime, endTime) {
let fullAge;
let startTimeArr = startTime.split("-");
let birthYear = startTimeArr[0];
let birthMonth = startTimeArr[1];
let birthDay = startTimeArr[2];
//ios 时间显示NAN,
let d = new Date(endTime.replace(/-/g, '/'));
let nowYear = d.getFullYear();
let nowMonth = d.getMonth() + 1;
let nowDay = d.getDate();
if (nowYear == birthYear) {
fullAge = 0; //同年 则为0岁
} else {
let ageDiff = nowYear - birthYear; //年之差
if (ageDiff > 0) {
if (nowMonth == birthMonth) {
let dayDiff = nowDay - birthDay; //日之差
if (dayDiff < 0) {
fullAge = ageDiff - 1;
} else {
fullAge = ageDiff;
}
} else {
let monthDiff = nowMonth - birthMonth; //月之差
if (monthDiff < 0) {
fullAge = ageDiff - 1;
} else {
fullAge = ageDiff;
}
}
} else {
fullAge = -1;
}
}
return fullAge; //返回周岁年龄
};