我在这里的逻辑上有一点困难,这已经很晚了,说实话,我很困惑。

我需要遍历时隙。

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


因此,我可以指定一个时隙间隔,因此,如果要迭代时隙(间隔为30分钟),它将为:

09:00
09:30
10:00
10:30


目前,我有以下内容:

$(document).ready(function () {

    getNumSlots(settings.minTime, settings.maxTime, settings.timeSlotGap);

    for(var i = 1; i<=settings.numSlots; i++){
        //I have no idea what I'm doing here
        $('#calendar').append("<p>Timeslot:" + i +"</p>");
    }


});
var WeekNo = moment().week();
var CurrentDate = getCurrentDate();
var WeekDay = moment().weekday();



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


if(settings.startOfWeek == 0){
    WeekDay = WeekDay - 1;
}


function getNumSlots(minTime, maxTime, timeSlotGap){
    var minTimeSplit = minTime.split(":");
    var hourStart = new Date("01/01/1900 " + minTime).getHours();
    var hourEnd = new Date("01/01/1900 " + maxTime).getHours();

    var minStart = new Date("01/01/1900 " + minTime).getMinutes();
    var minEnd = new Date("01/01/1900 " + maxTime).getMinutes();


    var diffHour = hourEnd - hourStart;
    var diffMins = minEnd - minStart;

    var slots = ((diffHour * 60) + diffMins) / timeSlotGap;

    settings.numSlots = slots;
}


如果它像循环30/60分钟那样简单,那不是问题,但是由于我可以为时隙指定任何内容(即90分钟),因此有点困难。

最佳答案

要获取时隙,您可以使用类似以下的内容:

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
);


这是一个示例JSFiddle

09-16 14:00