By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Find available time slots between events with Google Calendar API in js
JavaScript

Javascript – Find available time slots between events with Google Calendar API in js

Admin
Last updated: 2023/12/11 at 4:14 PM
Admin
Share
4 Min Read
Find available time slots between events with Google Calendar API in js

Problem:

I need to create an application with showing time slots for user.
I retrieve all events for day with Google API and let show example, already booked events:

Contents
Problem:Solution:SUGGESTIONOUTPUT
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') },
];

and I want to create available time slots between these events with interval 15 minutes and meeting duration should be 60 minutes
so based on that events I would like to have output:

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']

I need to create an application similar to booksy:

booksy planer

I tried something like that, but output is incorrect:

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

Please help! 😀

Solution:

SUGGESTION

I’ve made a slight tweak to the main logic operation on your function to check if a booking slot is available.

From:

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

To:

if (events.every(event => (currentSlotStart < event.startTime && currentSlotEnd <= event.startTime) || (currentSlotStart >= event.endTime))) {
      availableSlots.push({ start: currentSlotStart.toUTCString(), end: currentSlotEnd.toUTCString() });
    }
  1. The 1st logic was unchanged, since it already checks if the current timestamp (both the start and endpoint) does not conflict with an existing event’s starting time (this also covers for future event booking conflicts)

  2. I’ve changed the 2nd logic however, to just check if the current timestamp conflicts the ending point of a finished event; this is to validate that any finished events will not coincide with the booking time.

The full code snippet:

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

OUTPUT

image

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Some css properties aren't showing up when i use js/ts to make the stylesheet Javascript – Some css properties aren’t showing up when i use js/ts to make the stylesheet
Next Article Angular Previous page button getting disabled when the next page button gets clicked Javascript – Angular Previous page button getting disabled when the next page button gets clicked
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?