gpt4 book ai didi

javascript - 遍历时隙

转载 作者:行者123 更新时间:2023-11-28 19:11:41 26 4
gpt4 key购买 nike

我在这里的逻辑有点困难,现在已经晚了,老实说,我被难住了。

我需要循环遍历时间段。

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 .

关于javascript - 遍历时隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30630101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com