Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 545
ROKIKOKI Avatar asked May 22 '19 16:05

ROKIKOKI


2 Answers

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'

like image 144
TKoL Avatar answered Oct 19 '22 01:10

TKoL


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
like image 32
Hasta Tamang Avatar answered Oct 19 '22 01:10

Hasta Tamang