0

I have a list with busy times. With my code below I have managed to find the free time slots available within the busy times. However I want to find the free time slots based on a duration given. The code below generates my available gaps but I want to generate them based on duration. For example if the duration is 30 minutes, it should generate available times that can be 30 minutes.

var free_slots = []; 
var start_time = '08:00:00';
var end_time = '17:00:00';
busy_slots.sort(function(a, b){
    return new Date(a.Time_Start) > new Date(b.Time_Start)? 1: -1;
  });
 for(var i=0, l=busy_slots.length; i<l; i++){
    if(prettyDate2(new Date(busy_slots[i].Time_Start)) < start_time){
        busy_slots.splice(i, 1);
        break;
    }

}
for(var i=0, l=busy_slots.length; i<l; i++){
    end_time = busy_slots[i].Time_Start;
    if(i)
    start_time = busy_slots[i-1].Time_End;

    if((end_time && !i) || (end_time && i && busy_slots[i-1].Time_End < busy_slots[i].Time_Start) )
    free_slots.push({date: getDate(busy_slots[i].Time_End) ,start_time: start_time, end_time: end_time});

    if(i+1 === l){
        start_time = busy_slots[i].Time_End;

        if(start_time)
        free_slots.push({ date: getDate(busy_slots[i].Time_End), start_time: start_time, end_time: '17:00:00'});
      }

}
 return free_slots;
Celeste
  • 3
  • 1
  • If you already can manage available slots, with the start time and the end time, you just need to subtract them and get the duration to compare with what is required. – TGrif Feb 12 '19 at 17:52
  • As in, calculate the time diff between the start and end and then compare to the duration? var diff = start_time - end_time and then compare the diff to the duration? – Celeste Feb 12 '19 at 17:54

0 Answers0