JSFiddle - React, Tailwind, and code Playground

by jakeclarkson

JavaScript

var settings = {
    startOfWeek: 0, //0 = Sunday, 1 = Monday
    timeSlotGap: 30,
    minTime: "09:00:00",
    maxTime: "17:30:00",
    numSlots: 0
};

function getTimeDate(time) {
    var timeParts = time.split(':');
    var d = new Date();

    d.setHours(timeParts[0]);
    d.setMinutes(timeParts[1]);
    d.setSeconds(timeParts[2]);

    return d;
}

function getTimeSlots(startDate, endDate, interval) {
    var slots = [];

    var intervalMillis = interval * 60 * 1000;

    while (startDate < endDate) {
        // So that you get "00" if we're on the hour.
        var mins = (startDate.getMinutes() + '0').slice(0, 2);
        slots.push(startDate.getHours() + ':' + mins);
        startDate.setTime(startDate.getTime() + intervalMillis);
    }

    return slots;
}

var slots = getTimeSlots(getTimeDate(settings.minTime), getTimeDate(settings.maxTime), settings.timeSlotGap);

console.log(slots);