输入开始时间和结束时间以及时间间隔, 并以数组形式输出其中的时间段
const generateTimeSlots = (startHour: number, endHour: number, intervalMinutes: number) => {
const slots = [];
let currentTime = new Date().setHours(startHour, 0, 0, 0);
const endTime = new Date().setHours(endHour, 0, 0, 0);
while (currentTime < endTime) {
const start: Date = new Date(currentTime);
const end: Date = new Date((currentTime += intervalMinutes * 60000));
slots.push(
`${start.getHours().toString().padStart(2, '0')}:${start
.getMinutes()
.toString()
.padStart(2, '0')}` +
`-${end.getHours().toString().padStart(2, '0')}:${end
.getMinutes()
.toString()
.padStart(2, '0')}`,
);
}
return slots;
};
generateTimeSlots(8,16,30)
输出:['08:00-08:30', '08:30-09:00', '09:00-09:30', '09:30-10:00', '10:00-10:30', '10:30-11:00', '11:00-11:30', '11:30-12:00', '12:00-12:30', '12:30-13:00', '13:00-13:30', '13:30-14:00', '14:00-14:30', '14:30-15:00', '15:00-15:30', '15:30-16:00']