Find the available time slot with JavaScript

This is just a fun script to find the available time slot with JavaScript.
Let’s assume we have several time slots to provide our delivery service in the area. In the time slot we have start time and end time in just a plain string.
E.g.
var timeSlots = [ { 'time_range' : '07am - 09am' }, { 'time_range' : '09am - 11am' }, { 'time_range' : '11:00 AM - 01:00 PM' }, { 'time_range' : '10:00 PM - 11:00 PM' }, ];
Now, just get the current time hours and then filter the all slots to find the available slot. First finding the start time then add 12 to it if it includes the pm. Then compare time with current time and you will get the available time slot.
var currentTime = new Date().getHours(); var newSlot = timeSlots.filter(Slots => filterSlots(Slots)) function filterSlots(Slots) { var slot = Slots['time_range']; var startTime = slot.substring(0, slot.indexOf('-')); var isPM = startTime.toLocaleLowerCase().indexOf('pm'); var time = parseInt(startTime); if (isPM > -1) { time += 12; } if ( time >= currentTime ) { return slot; } } console.log('newSlot :>> ', newSlot);
Output is here.

It’s really a fun to find the available time slot with JavaScript. Hope you find this article useful.