Moment JS how to set is between and equal between 2 dates?

主宰稳场 提交于 2021-02-07 19:50:48

问题


Hi I would like to check if given date is between 2 dates with hours of course. So if we check if 13:00 is between 13:00 and 15:00 I would like to get true.

I have this code:

 let timeSlotStartTime = moment.utc(timeSlot.startTime);
    let startDayTime =  moment.utc(timeSlotStartTime.format('YYYY-MM-DD') + "T" + moment.utc(condition.parameters.timeFrom).format('HH:mm:00'));
    let endDayTime =  moment.utc(timeSlotStartTime.format('YYYY-MM-DD') + "T" + moment.utc(condition.parameters.timeTo).format('HH:mm:59'));
    return timeSlotStartTime.isBetween(startDayTime, endDayTime);

So I am checking if it is between but it works weird.

If I set hours of startDayTime = 14:59 and endDayTime = 17:00 and timeSlotStartTime is 14:15 it stills gives me true.(this is the bigger problem)

and if timeSlotStartTime = 17:00 gives me false (which I want to give me true)

Is there any possibility I can check between and equal within 2 moments?

thanks


回答1:


https://momentjscom.readthedocs.io/en/latest/moment/05-query/06-is-between/

You can use the 4th argument to include edges

moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '[)'); //true
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '(]'); //true
moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', null, '[]'); //true

Where square brackets means 'include this end' and parentheses means 'exclude this end'




回答2:


isBetween takes a third parameter (minutes, hours,days etc) and fourth parameter [] to include inclusivity

You should pass the third parameter as 'minutes' since you are comparing minutes

var timeSlotStartTime  = moment({ hour:14, minute:15 });
var startDayTime  = moment({ hour:14, minute:59 });
var endDayTime   = moment({ hour:17, minute:00 });


var result = timeSlotStartTime.isBetween(startDayTime, endDayTime, 'minutes', '[]');

console.log(result); // false

Also isBetween match is exclusive.

moment('2010-10-20').isBetween('2010-01-01', '2012-01-01', 'year'); // false as it is exclusive

Version 2.13.0 introduces inclusivity. A [ indicates inclusion of a value. A ( indicates exclusion. If the inclusivity parameter is used, both indicators must be passed.

moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '[)'); //true
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '(]'); //true
moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', null, '[]'); //true


来源:https://stackoverflow.com/questions/56260985/moment-js-how-to-set-is-between-and-equal-between-2-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!