I am working on creating an appointment application using momentjs and I don’t want to show the time that is already booked. I was able to do using a while loop but I have to pass by the index number for each time. Is there a shortcut way to do this if the bookedTime is more than 2? This is what I have so far. Any help will be appreciated:
const get24HoursTimeWithInterval = (
interval = 30,
startHour = 0,
endHour = 24,
bookedTime = [],
timeToPrepareForNextWork = 0,
) => {
const startTime = moment(startHour, 'h:mm A');
const endTime = moment(endHour, 'h:mm A');
const bookedBeginTime = moment(bookedTime[0], 'h:mm A');
const bookedEndTime = moment(bookedTime[1], 'h:mm A');
if (endTime.isBefore(startTime)) {
endTime.add(1, 'day');
}
const timeStops = [];
if (bookedTime.length > 0) {
while (startTime < bookedBeginTime) {
timeStops.push(new moment(startTime).format('h:mm A'));
startTime.add(interval + timeToPrepareForNextWork, 'minutes');
}
while (bookedEndTime < endTime) {
timeStops.push(new moment(bookedEndTime).format('h:mm A'));
bookedEndTime.add(interval + timeToPrepareForNextWork, 'minutes');
}
} else {
while (startTime <= endTime) {
timeStops.push(new moment(startTime).format('h:mm A'));
startTime.add(interval + timeToPrepareForNextWork, 'minutes');
}
}
const slots = timeStops;
return slots;
};
console.log(get24HoursTimeWithInterval(30, 0, 24,['12: 00 PM', '1:00 PM', '1:30 PM', '2:30 PM'], 10))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>