// 当前时间
let today = new Date()
// 生日
let birthYmd = new Date("1998-11-09")
const age = today.getFullYear() - birthYmd.getFullYear()
// 判断是否已过生日,如果未过生日则减少一岁
if (
today.getMonth() < birthYmd.getMonth() ||
(today.getMonth() === birthYmd.getMonth() && today.getDate() < birthYmd.getDate())
)
{
age--
}
console.log(age) // 年龄
JavaScript 根据生日计算年龄
JavaScript前端开发语言
1167

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



