Little verbose utility, you can use it..(有点冗长的实用程序,您可以使用它。)
var getTimeIntervals = function (time1, time2, slotInMinutes, workingHourStart, workingHourEnd) {
time1.setMinutes(0); time1.setSeconds(0);
var arr = [];
var workingHoursStart = workingHourStart;
var workingHourEnds = workingHourEnd;
var workingHourStartFloat = parseFloat("7:30");
var workingHourEndFloat = parseFloat("12:00");
while(time1 < time2){
var generatedSlot = time1.toTimeString().substring(0,5);
var generatedSlotFloat = parseFloat(generatedSlot);
time1.setMinutes(time1.getMinutes() + slotInMinutes);
if(generatedSlotFloat >= workingHourStartFloat && generatedSlotFloat < workingHourEndFloat){
var generatedObject = {
slot: time1.toTimeString().substring(0,5),
timeStamp: new Date(time1.getTime())
};
arr.push(generatedObject);
}
}
return arr;
}
var today = new Date();
var tomrorow = new Date().setDate(today.getDate()+1);
console.log(getTimeIntervals(today, tomorrow, 30, "7:30", "12:00"));
Function getTimeIntervals
expects startDate
, endDate
, slotDurationInMinutes
, workingHoursStart
and workingHourEnd
.(函数getTimeIntervals
期望startDate
, endDate
, slotDurationInMinutes
, workingHoursStart
和workingHourEnd
。)
Why I am returning object is because you may need the timestamp of selected slot in your further application use.(我之所以返回对象,是因为您可能需要在以后的应用程序使用中选择插槽的时间戳。)
Fiddle - https://jsfiddle.net/rahulrulez/t8ezfj2q/(小提琴-https: //jsfiddle.net/rahulrulez/t8ezfj2q/)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…