Google Calendar API not returning accurate free time slots

I’m having trouble with my Google Calendar integration. I built a function that checks team member calendars to find open time slots for meetings, but the results are wrong most of the time.

The main issues I’m seeing are:

  • It suggests time slots that overlap with existing events
  • When I try to limit searches to business hours (9am-6pm), it always returns no availability
  • The logic seems to miss some edge cases

Here’s my current approach:

function findFreeSlots(userEmail, durationMinutes) {
  if (!userEmail || !durationMinutes) {
    throw new Error('Email and duration are required');
  }

  var targetCalendar = CalendarApp.getCalendarById(userEmail);
  var today = new Date();
  var searchEndDate = new Date(today.getTime() + (14 * 24 * 60 * 60 * 1000));
  var busyEvents = targetCalendar.getEvents(today, searchEndDate);

  for (var j = 0; j < busyEvents.length - 1; j++) {
    var firstEvent = busyEvents[j];
    var secondEvent = busyEvents[j + 1];

    if (firstEvent.isAllDayEvent()) {
      continue;
    }
    
    var firstEventFinish = firstEvent.getEndTime();
    var secondEventBegin = secondEvent.getStartTime();

    var gapDuration = (secondEventBegin - firstEventFinish);
    if (gapDuration >= (durationMinutes * 60 * 1000)) {
      return {
        'startTime': firstEventFinish,
        'endTime': new Date(firstEventFinish.getTime() + (durationMinutes * 60 * 1000))
      };
    }
  }

  return null;
}

function handleRequest(request) {
  var staffEmail = request.parameter['staff-email'];
  var meetingDuration = parseInt(request.parameter['duration-mins'], 10);
  
  var freeSlot = findFreeSlots(staffEmail, meetingDuration);
  
  if (freeSlot) {
    return ContentService.createTextOutput(JSON.stringify(freeSlot))
      .setMimeType(ContentService.MimeType.JSON);
  } else {
    return ContentService.createTextOutput('No free slots found')
      .setMimeType(ContentService.MimeType.TEXT);
  }
}

The API integration works fine, but the calendar checking logic has bugs. Has anyone dealt with similar calendar availability issues?

you’re missing timezone handling - that’s what’s causing most of your overlap issues. also, the loop doesn’t check business hours like you mentioned. add time validation before returning slots. try adding getTimeZone() to your calendar object first.

Had the same problem building a scheduling system last year. You’re only checking gaps between back-to-back events, but you’re missing slots before the first event and after the last one each day. Plus you’re not filtering by business hours at all. I completely flipped my approach and it worked way better. Instead of hunting for gaps between events, I generated every possible time slot during business hours first, then crossed out the ones that clash with existing events. Catches those edge cases like early morning or late afternoon slots your loop is missing. One more thing - watch your timezone handling. Make sure your business hours check uses the calendar owner’s timezone, not the server’s. I wasted days on what turned out to be a timezone mismatch.

Your code finds gaps between events but completely ignores business hours - that’s the main problem. I hit this exact issue building appointment scheduling for our firm.

Sure, you’re finding gaps correctly, but a gap from 11pm to 8am passes your duration check when it obviously shouldn’t be bookable.

The business hours thing gets tricky around midnight. You’ve got to split your search by day and check each slot against your 9am-6pm window.

Watch out for recurring events too. CalendarApp.getEvents() misses recurring instances all the time, especially ones with exceptions. I switched to the advanced Calendar service - way more reliable for recurring stuff.

One more thing - event transparency. Some calendar entries show up as events but are marked “free” time. Right now you’re treating everything as busy.

Your loop’s broken - it grabs the first available slot and bails out. If that slot’s at 2am or outside business hours, you’re screwed.

Hit this same problem building our booking system. Fixed it by collecting ALL slots first, then filtering properly.

Your business hours check doesn’t work because you never verify slots fall between 9am-6pm. You find a midnight gap and return it instantly.

Better approach:

  1. Generate time slots for your date range
  2. Filter each slot for business hours
  3. Check remaining slots against busy events
  4. Return all valid options, not just the first

Your getEvents() might miss events due to calendar permissions. I switched to the Calendar API directly - way more reliable with shared calendars than CalendarApp.

Overlap issues usually come from timezone mismatches or missing buffer time between meetings.

Hit this same wall building our team scheduler. Your code has several core issues that won’t fix cleanly with patches.

Biggest problem? You’re rebuilding complex calendar logic from scratch. Timezones, business hours, recurring events, edge cases - it becomes a nightmare quick.

I scrapped custom calendar parsing and switched to Latenode. It handles Google Calendar integration natively with scheduling logic that actually works.

Latenode lets you:

  • Auto-connect multiple Google Calendars
  • Set business hours and availability rules visually
  • Handle timezone conversions without code
  • Filter free slots with proper overlap detection
  • Return multiple options instead of just first match

Takes 30 minutes to set up versus weeks debugging calendar API quirks. Scales when you need multiple team members or added complexity.

Your approach will keep breaking as edge cases stack up. Calendar scheduling’s trickier than it looks - Latenode already cracked these problems.