2

I have a case-scenario which I don't really know how to handle. I have two arrays of objects that contain date/time ranges. What I need to do is check if range from the first set, is contain in the rage of the second set, and if it is remove it. For example:

abc:[
   {
      "start": "2021-11-25 16:30:00",
      "end": "2021-11-25 17:30:00"
   }
]

xyz:[
   {
      "start": "2021-11-25 09:00:00",
      "end": "2021-11-25 18:00:00"
   },
   {
      "start": "2021-11-26 15:00:00",
      "end": "2021-11-26 19:00:00"
   }
]

I need to remove the range of abc from xyz, so in the end the xyz would look like so:

xyz:[
   {
      "start": "2021-11-25 09:00:00",
      "end": "2021-11-25 16:30:00"
   },
   {
      "start": "2021-11-25 17:30:00",
      "end": "2021-11-25 18:00:00"
   },
   {
      "start": "2021-11-26 15:00:00",
      "end": "2021-11-26 19:00:00"
   }
]

this can be done either in PHP or JS.

Google so far was not very hepfull.

Thanks in advance for any kind of help

Peter
  • 181
  • 2
  • 12

1 Answers1

1

eheh the day that google will be able to solve algorithms all software developers disapear. here's some fast javascript:

        
var abc=[
   {
      "start": "2021-11-25 16:30:00",
      "end": "2021-11-25 17:30:00"
   }
]

var xyz =[
   {
      "start": "2021-11-25 09:00:00",
      "end": "2021-11-25 18:00:00"
   },
   {
      "start": "2021-11-26 15:00:00",
      "end": "2021-11-26 19:00:00"
   }
];

var newXyz=[];
for(var i in xyz){
  var start = new Date(xyz[i]["start"]).getTime();
  var end = new Date(xyz[i]["end"]).getTime();
  var collisionDetected=false;
  for(var j in abc){
    var start2 = new Date(abc[j]["start"]).getTime();
    var end2 = new Date(abc[j]["end"]).getTime();
    if(
      start2>start &&
      start2<end &&
      1
    ){
      newXyz.push({"start":xyz[i]["start"],"end":abc[j]["start"]});

      if(
        end2<end &&
        1
      ){
        newXyz.push({"start":abc[j]["end"],"end":xyz[i]["end"]});
      }
      collisionDetected=true;
      break;
    }
  }
  if(collisionDetected==false){
    newXyz.push({"start":xyz[i]["start"],"end":xyz[i]["end"]});
  }
}

xyz = newXyz;

console.dir(xyz);
RobG
  • 142,382
  • 31
  • 172
  • 209
emilianoc
  • 11
  • 2
  • Re `new Date(xyz[i]["start"])` see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) (hint: `new Date("2021-11-26 15:00:00")` produces an invalid date in Safari). Given the strings are in ISO 8601–ish format, they can be compared as strings, there's no need to convert to Date objects anyway. A good answer should explain how the code works, either as text or comments (not to mention that the code doesn't work in Safari). :-( – RobG Nov 26 '21 at 01:08
  • Also, `start2 – RobG Nov 26 '21 at 01:19
  • i'm pretty new on stackoverflow so next time i try to be more accuratewith the world fast javascript i mean that the code is not fully tested. i don't like comment single lines, if the intent of the code is clear single line comment are redundant in my opinion (but i try to comment more and test more thanks for the advices). "start2 – emilianoc Nov 26 '21 at 08:50
  • @emilianoc - almost. It works perfectly fine, but only if there is a single collision in a day, If abc contained 2 entries, both for the same day (ie. 10-12 & 14-15) only the first one is taken into consideration. – Peter Nov 29 '21 at 10:50