以月为单位的时刻差异,不包括天数
Moment diff in months excluding days
我想在不考虑天数的情况下算出两个日期之间的月差。
我试图使用 Math.ceil
,但如果 2022 年的那一天比 2021 年的那一天早,那么我得到 2 个月而不是 1 个月的差异。
const diff = moment([2022, 0, 1]).diff(moment([2021, 11, 3]), 'months', true);
console.log(diff); // 0.935483870967742, expected 1
const diffCeil = Math.ceil(
moment([2022, 0, 3]).diff(moment([2021, 11, 1]), 'months', true)
);
console.log('diffCeil', diffCeil); // 2, expected 1
// Inaccurate diff doesn't work when 2021 day is bigger than in 2022
const inacurrateDiff = moment([2022, 0, 3]).diff(moment([2021, 11, 1]), 'months');
console.log('inacurrateDiff', inacurrateDiff); // 1, as expected
const inacurrateDiff2 = moment([2022, 0, 1]).diff(moment([2021, 11, 3]), 'months');
console.log('inacurrateDiff2', inacurrateDiff2); // 0, expected 1
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>
我确实尝试过使用和不使用 diff
的第三个 precise
参数。
你得到 2 是因为你使用的是 Math.ceil
,它总是将小数值提高到下一个整数。
这里没有一个正确答案,您必须决定要计算小数月的多少。
例如,如果您只需要 整个 个月,请使用 Math.floor
而不是 Math.ceil
。如果您想将 1.5 个月以内的任何内容计为 1,但 1.5 个月及以上的时间计为 2,请使用 Math.round
。或者您可以设置自己的阈值,例如 1.2(这需要更多工作,但不是那么多)。
示例:
function example(date1, date2) {
const rawDiff = date1.diff(date2, "months", true);
const diffFloored = Math.floor(rawDiff);
const diffRounded = Math.round(rawDiff);
const diffCeiled = Math.ceil(rawDiff);
const neg = rawDiff < 0;
const fractional = neg
? rawDiff + Math.trunc(rawDiff)
: rawDiff - Math.trunc(rawDiff);
const diffOnePointTwo = Math.abs(fractional) > 0.2
? Math.ceil(rawDiff)
: Math.floor(rawDiff);
console.log({
date1: date1.toString(),
date2: date2.toString(),
rawDiff,
diffFloored,
diffRounded,
diffCeiled,
diffOnePointTwo,
});
}
example(
moment([2022, 0, 1]),
moment([2021, 11, 3])
);
example(
moment([2022, 0, 3]),
moment([2021, 11, 1])
);
example(
moment([2022, 0, 10]),
moment([2021, 11, 1])
);
.as-console-wrapper {
max-height: 100% !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>
只有您和您的项目可以说出您想使用的定义。
您可以使用 startOf(d, 'month')
比较月初:
a.startOf('month').diff(b.startOf('month'), 'month');
我想在不考虑天数的情况下算出两个日期之间的月差。
我试图使用 Math.ceil
,但如果 2022 年的那一天比 2021 年的那一天早,那么我得到 2 个月而不是 1 个月的差异。
const diff = moment([2022, 0, 1]).diff(moment([2021, 11, 3]), 'months', true);
console.log(diff); // 0.935483870967742, expected 1
const diffCeil = Math.ceil(
moment([2022, 0, 3]).diff(moment([2021, 11, 1]), 'months', true)
);
console.log('diffCeil', diffCeil); // 2, expected 1
// Inaccurate diff doesn't work when 2021 day is bigger than in 2022
const inacurrateDiff = moment([2022, 0, 3]).diff(moment([2021, 11, 1]), 'months');
console.log('inacurrateDiff', inacurrateDiff); // 1, as expected
const inacurrateDiff2 = moment([2022, 0, 1]).diff(moment([2021, 11, 3]), 'months');
console.log('inacurrateDiff2', inacurrateDiff2); // 0, expected 1
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>
我确实尝试过使用和不使用 diff
的第三个 precise
参数。
你得到 2 是因为你使用的是 Math.ceil
,它总是将小数值提高到下一个整数。
这里没有一个正确答案,您必须决定要计算小数月的多少。
例如,如果您只需要 整个 个月,请使用 Math.floor
而不是 Math.ceil
。如果您想将 1.5 个月以内的任何内容计为 1,但 1.5 个月及以上的时间计为 2,请使用 Math.round
。或者您可以设置自己的阈值,例如 1.2(这需要更多工作,但不是那么多)。
示例:
function example(date1, date2) {
const rawDiff = date1.diff(date2, "months", true);
const diffFloored = Math.floor(rawDiff);
const diffRounded = Math.round(rawDiff);
const diffCeiled = Math.ceil(rawDiff);
const neg = rawDiff < 0;
const fractional = neg
? rawDiff + Math.trunc(rawDiff)
: rawDiff - Math.trunc(rawDiff);
const diffOnePointTwo = Math.abs(fractional) > 0.2
? Math.ceil(rawDiff)
: Math.floor(rawDiff);
console.log({
date1: date1.toString(),
date2: date2.toString(),
rawDiff,
diffFloored,
diffRounded,
diffCeiled,
diffOnePointTwo,
});
}
example(
moment([2022, 0, 1]),
moment([2021, 11, 3])
);
example(
moment([2022, 0, 3]),
moment([2021, 11, 1])
);
example(
moment([2022, 0, 10]),
moment([2021, 11, 1])
);
.as-console-wrapper {
max-height: 100% !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>
只有您和您的项目可以说出您想使用的定义。
您可以使用 startOf(d, 'month')
比较月初:
a.startOf('month').diff(b.startOf('month'), 'month');