Skip to content

Instantly share code, notes, and snippets.

@almgwary
Created January 15, 2018 14:35
Show Gist options
  • Save almgwary/d7a06e7e728e2643032fdf6aa49ea25d to your computer and use it in GitHub Desktop.
Save almgwary/d7a06e7e728e2643032fdf6aa49ea25d to your computer and use it in GitHub Desktop.
change slot of [from][to] filed into slots of range equal averageTimetime
const sliceSlotsByAverageTime = (slots,averageTime)=>{
let newSlots = [] ;
// loop over slots
slots.forEach((slot)=>{
let from = new Date(slot.from);
let end = new Date(slot.to);
let loop = 1 ;
while((from.getTime() + averageTime * 1000 * 60 ) <= end.getTime() && loop++ < 9000){
console.log(from.toString())
let _from = new Date(from) ;
let _to = new Date(from.setMinutes(from.getMinutes() + averageTime)) ;
let freeSlot = {from:_from,to:_to};
newSlots.push(freeSlot);
//from.setMinutes(from.getMinutes() + averageTime);
}
});
return newSlots;
}
let dates = [ {
"from": "2018-01-15T09:00:00.000Z",
"to": "2018-01-15T23:00:00.000Z",
"serviceId": "5a35577c6fb89000141de146",
"type": "FREE"
},
{
"from": "2018-01-16T10:00:00.000Z",
"to": "2018-01-16T23:20:00.000Z",
"serviceId": "5a35577c6fb89000141de146",
"type": "FREE"
}];
freeSlotsToAverageTime(dates,30);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment