How to get current events from google calendar API

2019-07-16 14:55发布

问题:

I'm trying to use the Google Calendar API method gapi.client.calendar.events.list to retrieve events on the calendar now.

I can successfully filter on start date and end date. But, filtering on things that start today doesn't help me if an event started yesterday and is still on-going.

    var request = gapi.client.calendar.events.list({
      'calendarId': 'my calendar id',
      // shows things that have not yet started
      'timeMin': (new Date()).toISOString(),
      'showDeleted': false,
      'singleEvents': true,
      'orderBy': 'startTime'
    });

How do I find the list of things on a calendar that are currently going on?

I'd (obviously) like to avoid pulling a week's worth of events and parsing through it.

回答1:

There isn't a way of querying for the current event directly, but you can certainly give it a limited min and max time, so you'll only have a small number of events to search through. If your events are regular and of predictable duration, you may be able to make it just give you one.

events = client.events().list(calendarId='primary',
                              timeMin='2011-12-22T09:00:00Z',
                              timeMax='2011-12-22T22:00:00Z').execute()

For more information, please read the Official Documentation of Google Calendar API: https://developers.google.com/google-apps/calendar/?csw=1