moment.isBetween() 未按预期工作
moment.isBetween() is not working as expected
我正在使用 moment(版本“^2.25.3”)库来检查日期是否在两个日期之间。
我必须检查 araDate 是否在两个日期(startdate 和 endDate)之间或 araDateMinusOne 是否在两个日期(startDate 和 endDate)之间,但这里的条件是 startdate 应该等于 araDateMinusOne(即如果 a开始日期是 sep/03,结束日期是 sep/04,它们不应该匹配)。
有人可以告诉我我做错了什么或者为什么会这样吗?
const obj = {
_id: '614c2b941e06b7003024e1df',
promotedtitles: [{ title: '1' }, { title: '2' }],
startdate: '2021-09-04T00:00:00.000Z',
enddate: '2021-09-04T00:00:00.000Z'
};
const camStartDate = moment(obj.startdate).format('YYYY-MM-DD'); // 2021-09-04
const camEndDate = moment(obj.enddate).format('YYYY-MM-DD'); // 2021-09-04
const araDate = moment('2021-09-05T00:00:00.000Z').format('YYYY-MM-DD'); // 2021-09-05
const araDateMinusOne = moment('2021-09-05T00:00:00.000Z').subtract(1, 'day').format('YYYY-MM-DD'); // 2021-09-04
console.log('check', moment(araDate).isBetween(camStartDate, camEndDate, undefined, '[]')); // false which is fine.
console.log('another check', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, undefined, '[)')); // this is consoling false, which is incorrect
//I also have tried this as well and all the variations.
console.log('another check', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, 'day', '[)'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.3/moment.min.js" ></script>
检查相等范围并给出'[)'作为参数返回false的原因是enddate在这里优先。当它检查 endDate 时,我们为此提供了排除运算符。
moment('2021-09-04').isBetween('2021-09-04', '2021-09-04', 'day', '[)'
此问题的解决方案是检查日期是否介于两个日期之间以及开始日期是否等于 araMinusOne 日期。
moment(araDateMinusOne).isBetween(camStartDate, camEndDate, 'day', '[]') && moment(araDateMinusOne).isSame(camStartDate)
包含参数应该是[]
const obj = {
_id: '614c2b941e06b7003024e1df',
promotedtitles: [{ title: '1' }, { title: '2' }],
startdate: '2021-09-04T00:00:00.000Z',
enddate: '2021-09-04T00:00:00.000Z'
};
const camStartDate = moment(obj.startdate).format('YYYY-MM-DD'); // 2021-09-04
const camEndDate = moment(obj.enddate).format('YYYY-MM-DD'); // 2021-09-04
const araDate = moment('2021-09-05T00:00:00.000Z').format('YYYY-MM-DD'); // 2021-09-05
const araDateMinusOne = moment('2021-09-05T00:00:00.000Z').subtract(1, 'day').format('YYYY-MM-DD'); // 2021-09-04
console.log('check', moment(araDate).isBetween(camStartDate, camEndDate, undefined, '[]')); // false which is fine.
console.log({araDateMinusOne,camStartDate, camEndDate})
console.log('another check', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, undefined, '[]')); // this is consoling false, which is incorrect
//I also have tried this as well and all the variations.
console.log('another check 1', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, 'day', '[)'));
console.log('default check ', moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', undefined, '[]'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.3/moment.min.js" ></script>
我正在使用 moment(版本“^2.25.3”)库来检查日期是否在两个日期之间。
我必须检查 araDate 是否在两个日期(startdate 和 endDate)之间或 araDateMinusOne 是否在两个日期(startDate 和 endDate)之间,但这里的条件是 startdate 应该等于 araDateMinusOne(即如果 a开始日期是 sep/03,结束日期是 sep/04,它们不应该匹配)。
有人可以告诉我我做错了什么或者为什么会这样吗?
const obj = {
_id: '614c2b941e06b7003024e1df',
promotedtitles: [{ title: '1' }, { title: '2' }],
startdate: '2021-09-04T00:00:00.000Z',
enddate: '2021-09-04T00:00:00.000Z'
};
const camStartDate = moment(obj.startdate).format('YYYY-MM-DD'); // 2021-09-04
const camEndDate = moment(obj.enddate).format('YYYY-MM-DD'); // 2021-09-04
const araDate = moment('2021-09-05T00:00:00.000Z').format('YYYY-MM-DD'); // 2021-09-05
const araDateMinusOne = moment('2021-09-05T00:00:00.000Z').subtract(1, 'day').format('YYYY-MM-DD'); // 2021-09-04
console.log('check', moment(araDate).isBetween(camStartDate, camEndDate, undefined, '[]')); // false which is fine.
console.log('another check', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, undefined, '[)')); // this is consoling false, which is incorrect
//I also have tried this as well and all the variations.
console.log('another check', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, 'day', '[)'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.3/moment.min.js" ></script>
检查相等范围并给出'[)'作为参数返回false的原因是enddate在这里优先。当它检查 endDate 时,我们为此提供了排除运算符。
moment('2021-09-04').isBetween('2021-09-04', '2021-09-04', 'day', '[)'
此问题的解决方案是检查日期是否介于两个日期之间以及开始日期是否等于 araMinusOne 日期。
moment(araDateMinusOne).isBetween(camStartDate, camEndDate, 'day', '[]') && moment(araDateMinusOne).isSame(camStartDate)
包含参数应该是[]
const obj = {
_id: '614c2b941e06b7003024e1df',
promotedtitles: [{ title: '1' }, { title: '2' }],
startdate: '2021-09-04T00:00:00.000Z',
enddate: '2021-09-04T00:00:00.000Z'
};
const camStartDate = moment(obj.startdate).format('YYYY-MM-DD'); // 2021-09-04
const camEndDate = moment(obj.enddate).format('YYYY-MM-DD'); // 2021-09-04
const araDate = moment('2021-09-05T00:00:00.000Z').format('YYYY-MM-DD'); // 2021-09-05
const araDateMinusOne = moment('2021-09-05T00:00:00.000Z').subtract(1, 'day').format('YYYY-MM-DD'); // 2021-09-04
console.log('check', moment(araDate).isBetween(camStartDate, camEndDate, undefined, '[]')); // false which is fine.
console.log({araDateMinusOne,camStartDate, camEndDate})
console.log('another check', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, undefined, '[]')); // this is consoling false, which is incorrect
//I also have tried this as well and all the variations.
console.log('another check 1', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, 'day', '[)'));
console.log('default check ', moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', undefined, '[]'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.3/moment.min.js" ></script>