javascript - Get the next instance of recurring events with the Google Calendar API v3 - Stack Overflow

I am trying to get the events in my calendar over a date range (single or recurring events alike).Now

I am trying to get the events in my calendar over a date range (single or recurring events alike). Now I will not necessarily know the exact datetime for the next instance of a recurring event as . For recurring events I want the date of the next occurrence rather than the date of the first instance as I am getting with the request below. How do I do this?

/{calendarid}/events?fields=items(id,summary,start)&key={APIkey}&timeMax={enddate}&timeMin={startdate}

I am trying to get the events in my calendar over a date range (single or recurring events alike). Now I will not necessarily know the exact datetime for the next instance of a recurring event as . For recurring events I want the date of the next occurrence rather than the date of the first instance as I am getting with the request below. How do I do this?

https://www.googleapis./calendar/v3/calendars/{calendarid}/events?fields=items(id,summary,start)&key={APIkey}&timeMax={enddate}&timeMin={startdate}

Share Improve this question edited Jan 25, 2013 at 13:24 Sola Oderinde asked Jan 25, 2013 at 12:47 Sola OderindeSola Oderinde 1,0562 gold badges22 silver badges30 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

Add in the URL parameter singleEvents=True. This will expand all recurring events into their individual items. There isn't any way to specifically get just the second event in a series if you don't already know when it is, but using that parameter will let you parse out all instances and get the one you want.

Source: https://developers.google./google-apps/calendar/v3/reference/events/list

long now = System.currentTimeMillis();

Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(eventsUriBuilder, Long.MIN_VALUE);
ContentUris.appendId(eventsUriBuilder, Long.MAX_VALUE);
String[] projection = new String[]{CalendarContract.Instances.CALENDAR_ID, CalendarContract.Instances.TITLE,
        CalendarContract.Instances.DESCRIPTION, CalendarContract.Instances.BEGIN,
        CalendarContract.Instances.END, CalendarContract.Instances.EVENT_LOCATION,
        CalendarContract.Instances.EVENT_ID};

Uri eventsUri = eventsUriBuilder.build();
Cursor instance_cursor = getActivity().getContentResolver().query(
        eventsUri, projection, CalendarContract.Instances.BEGIN + " >= " + now + " and " + CalendarContract.Instances.BEGIN
                + " <= " + (now + 2592000000L) + " and " + CalendarContract.Instances.VISIBLE + " = 1",
        null, CalendarContract.Instances.DTSTART + " ASC");

There is now direct way of doing that but you can hack together a method to acplish that.

The idea is that you can specify time boundaries and limit the number of records and that's enough to do the job.

So setting the timeMin to now (or right after your reference event end date), timeMax to a big enough number, like a year from timeMin and maxResults = 1 will get the next instance if it exists.

Here is how this can be done in nodejs:

    let startDate = startTime ? new Date(startTime) : new Date()
    let endDate = endTime ? new Date(endTime) : new Date(startDate.getTime() + 12 * 31 * 24 * 60 * 60 * 1000) // 1 year ahead
    let startDateISO = startDate.toISOString()
    let endDateISO = endDate.toISOString()

    let result = await  calendar.events.instances({
        calendarId: "primary",
        eventId: masterEventId,
        maxResults: 1,
        timeMin: startDateISO,
        timeMax: endDateISO,
        orderBy: "startTime",
        timeZone: "UTC"
    })
    let nextInstance = result.data.items[0]

Similarly you can get previous instance if you order results in descending order and reverse the time boundaries (set timeMin to a year back and timeMax before the start of the reference event).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743747509a4500280.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信