Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use momentjs to check if this time range is conflict with other time range

Scenario 1

Time range 1 : 2016-12-06 11:00 to 2016-12-06 12:00
Time range 2 : 2016-12-06 10:00 to 2016-12-06 13:00
time range 1 is completely conflict with time range 2 and vice versa

Scenario 2

Time range 1 : 2016-12-06 11:00 to 2016-12-06 12:00
Time range 2 : 2016-12-06 11:00 to 2016-12-06 14:00
time range 1 is partial conflict with time range 2 and vice versa

Scenario 3

Time range 1 : 2016-12-06 11:00 to 2016-12-06 12:00
Time range 2 : 2016-12-06 09:00 to 2016-12-06 12:00
time range 1 is partial conflict with time range 2 and vice versa

How to accomplish above scenario using momentjs? I tried the isBetween function but couldn't get it work.

like image 540
Mark Thien Avatar asked Dec 05 '16 08:12

Mark Thien


1 Answers

I think moment-range is what you are looking for. Use its overlaps and contains function to make it. You can link to moment-range from the cdnjs servers.

var date1 = [moment("2016-12-06 11:00"), moment("2016-12-06 12:00")];
var date2 = [moment("2016-12-06 10:00"), moment("2016-12-06 13:00")];

var range  = moment.range(date1);
var range2 = moment.range(date2);

// has overlapping
if(range.overlaps(range2)) {
    if((range2.contains(range, true) || range.contains(range2, true)) && !date1[0].isSame(date2[0]))
    alert("time range 1 is completely conflict with time range 2 and vice versa");
  else
    alert("time range 1 is partially conflict with time range 2 and vice versa");
}
like image 166
ichbinblau Avatar answered Oct 07 '22 17:10

ichbinblau