【发布时间】:2019-10-11 09:16:21
【问题描述】:
嘿,我正在尝试使用 Moment 计算用户年龄。
function getAge(birthDate) {
const start = moment(birthDate, "YYYY-MM-DD");
const end = moment();
return start.to(end, true);
}
5 月之前,结果超过 1 年:
getAge("1989-01-01"); // 31 years
getAge("1989-05-01"); // 30 years
示例:https://codesandbox.io/s/nostalgic-tesla-bzb3d
使用fromNow 而不是to 的行为是相同的
你知道如何解决这个问题吗?
【问题讨论】:
-
是因为四舍五入到最接近的整数年份
标签: javascript momentjs