javascript - Google Calendar API: get list of free slots on specified day - Stack Overflow

admin2025-04-19  0

I need to get list of free slots in my google calendar. Now I am just getting list of events. I am using google calendar npm.

google_calendar.events.list(calObj.name,{
    timeMin: "2018-03-02T08:00:00.000Z",
    timeMax: "2018-03-02T16:00:00.000Z",
    singleEvents: true,
    orderBy: "startTime"
}, function (err, eventList) {
    // handle to get output like
    // freeSlots -> [{
    //     "startDate": "2018-03-02T08:00:00.000Z",
    //     "endDate": "2018-03-02T09:00:00.000Z"
    // },{
    //     "startDate": "2018-03-02T07:00:00.000Z",
    //     "endDate": "2018-03-02T08:00:00.000Z"
    // }]

    // if at this day are events between 10:00 and 16:00 (so calendar is busy)
})

I need to get list of free slots in my google calendar. Now I am just getting list of events. I am using google calendar npm.

google_calendar.events.list(calObj.name,{
    timeMin: "2018-03-02T08:00:00.000Z",
    timeMax: "2018-03-02T16:00:00.000Z",
    singleEvents: true,
    orderBy: "startTime"
}, function (err, eventList) {
    // handle to get output like
    // freeSlots -> [{
    //     "startDate": "2018-03-02T08:00:00.000Z",
    //     "endDate": "2018-03-02T09:00:00.000Z"
    // },{
    //     "startDate": "2018-03-02T07:00:00.000Z",
    //     "endDate": "2018-03-02T08:00:00.000Z"
    // }]

    // if at this day are events between 10:00 and 16:00 (so calendar is busy)
})
Share Improve this question edited Jul 15, 2018 at 21:15 ifeelmyself asked Mar 2, 2018 at 17:06 ifeelmyselfifeelmyself 1071 silver badge11 bronze badges 2
  • AFAIK, there is no API endpoint to get a list of free slots on a specific day. A work around would be is to getting the list of events for the day then arrange it from the beginning of the day to the end. Calculate the time in between the event. Then there you have the free slot on that specific day. Hope this helps. – Mr.Rebot Commented Mar 3, 2018 at 12:09
  • first you'd have to define "slot". The calendar does not have fixed "slots". An event can be as long or short as you like, and start / finish at any time you like, so your question does not really make sense. – ADyson Commented Mar 12, 2018 at 14:00
Add a ment  | 

1 Answer 1

Reset to default 5

You can get free slots from google calendar in two steps. Using npm google-calendar

1st you have to get all free/busy times in your calendar

var startDate = new Date(),
    endDate = new Date();

var rootStart = startDate,
    rootEnd = endDate;

gcal(<accessToken>).freebusy.query({
    "items":[{
        "id": calObj.name
    }],
    "timeMin": startDate.toISOString(),
    "timeMax": endDate.toISOString(),
    "timeZone": "GMT+0100"
},{
    fields: "calendars,groups,kind,timeMax,timeMin", 
    alt:"json"
}, function(err, data) {
    if(err) return console.log(err)

    // then calculate free slots
    return slotsFromEvents(startDate, data.calendars[<calName>].busy)
})

var interval = 2, // how big single slot should be (in this case 2 hrs) 
freeSlots = []; 

function slotsFromEvents(date,events) {
    events.forEach(function (event, index) { //calculate free from busy times
        if (index == 0 && startDate < event.start) {
            freeSlots.push({startDate: startDate, endDate: event.start});
        }
        else if (index == 0) {
            startDate = event.end;
        }
        else if (events[index - 1].end < event.start) {
            freeSlots.push({startDate: events[index - 1].end, endDate: event.start});
        }

        if (events.length == (index + 1) && event.end < endDate) {
            freeSlots.push({startDate: event.end, endDate: endDate});
        }
    });


    if (events.length == 0) {
        freeSlots.push({startDate: startDate, endDate: endDate});
    }

    var temp = {}, hourSlots = [];
    freeSlots.forEach(function(free, index) {
        var freeHours = new Date(free.endDate).getHours() - new Date(free.startDate).getHours(), freeStart = new Date(free.startDate), freeEnd = new Date(free.endDate);
        while(freeStart.getHours()+freeHours+interval>=0) { // 11 + 4 + 2 >= 0
            if(freeHours>=interval) {
                temp.e = new Date(free.startDate);
                temp.e.setHours(temp.e.getHours()+freeHours);
                temp.s = new Date(free.startDate);
                temp.s.setHours(temp.s.getHours()+freeHours-interval);
                if(temp.s.getHours() >= rootStart.getHours() && temp.e.getHours() <= rootEnd.getHours()) {
                    hourSlots.push({calName: calObj.name, startDate:temp.s, endDate:temp.e});
                    temp = {};
                }
            }
            freeHours--;
        }
    })

    // callBack(freeSlots, hourSlots);
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745021965a280433.html

最新回复(0)