使用 js 中的 Google Calendar API 查找事件之间的可用时间段

问题描述 投票:0回答:1

我需要创建一个为用户显示时间段的应用程序。 我使用 Google API 检索当天的所有活动并展示示例,已预订的活动:

const events = [
  { startTime: new Date('2023-10-18T00:00:00+0000'), endTime: new Date('2023-10-18T08:00:00+0000') },
  { startTime: new Date('2023-10-18T09:00:00+0000'), endTime: new Date('2023-10-18T10:00:00+0000') },
  { startTime: new Date('2023-10-18T11:00:00+0000'), endTime: new Date('2023-10-18T12:30:00+0000') },
  { startTime: new Date('2023-10-18T16:00:00+0000'), endTime: new Date('2023-10-18T17:30:00+0000') },
  { startTime: new Date('2023-10-18T18:00:00+0000'), endTime: new Date('2023-10-19T00:00:00+0000') },
];

我想在这些活动之间创建可用的时间段,间隔为 15 分钟,会议持续时间应为 60 分钟 所以基于这些事件我想要输出:

const availableSlots = ['8:00', '10:00', '12:30', '12:45', '13:00', '13:15', '13:30', '13:45', '14:00', '14:15', '14:30', '14:45', '15:00']

我需要创建一个类似于 booksy 的应用程序:

我尝试了类似的方法,但输出不正确:

const meetingDuration = 60; // minutes
const interval = 15; // minutes
const workdayStart = new Date('2023-10-18T08:00:00'); // Start of workday
const workdayEnd = new Date('2023-10-18T17:00:00'); // End of workday

const availableSlots = [];

let currentSlotStart = new Date(workdayStart);

while (currentSlotStart < workdayEnd) {
  const currentSlotEnd = new Date(currentSlotStart);
  currentSlotEnd.setMinutes(currentSlotEnd.getMinutes() + meetingDuration);

  if (events.every(event => (currentSlotStart < event.startTime && currentSlotEnd <= event.startTime) || (currentSlotEnd >= event.endTime && currentSlotEnd >= event.endTime))) {
    availableSlots.push({ start: currentSlotStart.toUTCString(), end: currentSlotEnd.toUTCString() });
  }

  currentSlotStart = new Date(currentSlotStart);
  currentSlotStart.setMinutes(currentSlotStart.getMinutes() + interval);
}

console.log(availableSlots);

请帮忙! :D

javascript date time google-calendar-api
1个回答
0
投票

建议

我对您的函数的主要逻辑操作进行了轻微调整,以检查预订时段是否可用。

来自:

if (events.every(event => (currentSlotStart < event.startTime && currentSlotEnd <= event.startTime) || (currentSlotEnd >= event.endTime && currentSlotEnd >= event.endTime))) {
    availableSlots.push({ start: currentSlotStart.toUTCString(), end: currentSlotEnd.toUTCString() });
  }

致:

if (events.every(event => (currentSlotStart < event.startTime && currentSlotEnd <= event.startTime) || (currentSlotStart >= event.endTime))) {
      availableSlots.push({ start: currentSlotStart.toUTCString(), end: currentSlotEnd.toUTCString() });
    }
  1. 第一个逻辑未更改,因为它已经检查当前时间戳(开始和结束点)是否与现有活动的开始时间不冲突(这也涵盖了未来活动预订冲突)

  2. 但是,我更改了第二个逻辑,只是检查当前时间戳是否与已完成事件的结束点冲突;这是为了验证任何已完成的活动都不会与预订时间重合。

完整代码片段:

  const meetingDuration = 60; // minutes
  const interval = 15; // minutes
  // just added UTC timezone stamp for consistency
  const workdayStart = new Date('2023-10-18T08:00:00+0000'); // Start of workday
  const workdayEnd = new Date('2023-10-18T17:00:00+0000'); // End of workday

  const availableSlots = [];

  let currentSlotStart = new Date(workdayStart);

  while (currentSlotStart < workdayEnd) {
    const currentSlotEnd = new Date(currentSlotStart);
    currentSlotEnd.setMinutes(currentSlotEnd.getMinutes() + meetingDuration);

    if (events.every(event => (currentSlotStart < event.startTime && currentSlotEnd <= event.startTime) || (currentSlotStart >= event.endTime))) {
      availableSlots.push({ start: currentSlotStart.toUTCString() });
    }

    currentSlotStart = new Date(currentSlotStart);
    currentSlotStart.setMinutes(currentSlotStart.getMinutes() + interval);
  }

  console.log(availableSlots);

输出

© www.soinside.com 2019 - 2024. All rights reserved.