const calculateAge = (date) => {
const currentDate = new Date();
const birthDateObj = new Date(date);
birthDateObj.setHours(0, 0, 0, 0);
currentDate.setHours(0, 0, 0, 0);
const age = currentDate.getFullYear() - birthDateObj.getFullYear();
const currentMonth = currentDate.getMonth();
const birthMonth = birthDateObj.getMonth();
if (currentMonth < birthMonth || (currentMonth === birthMonth && currentDate.getDate() < birthDateObj.getDate())) {
return age - 1;
} else {
return age;
}
}
JS根据日期获取当前年龄
于 2023-09-06 14:30:12 首次发布