Lakshay Narang
Lakshay Narang

Reputation: 3

How Can I Get List Of Ended Events From Google Calendar API

Here is the code:

const calendar = google.calendar({ version: 'v3', auth });

const meetingsResponse = await calendar.events.list({
      calendarId: 'primary',
      timeMin: new Date(new Date().setHours(0,0,0,0)).toISOString(),
      maxResults: 1,
      singleEvents: true,
      orderBy: 'startTime',
     });

Output: ?

Do someone know where is the problem?

Upvotes: 0

Views: 586

Answers (1)

Kristkun
Kristkun

Reputation: 5953

You can set the timeMax to the current datetime to get a list of ended events. If you really want to get only 1 result (latest ended event) you can retain maxResults to 1

timeMax

  • Upper bound (exclusive) for an event's start time to filter by.
  • This will the the maximum date time that the event's start time should have configured.

timeMin

  • Lower bound (exclusive) for an event's end time to filter by
  • This will the the minimum date time that the event's end time should have configured.

For Example:

timeMax = 2020-12-23T11:00:00Z (datetime now)

Event 1 start time = 2020-12-22T08:00:00Z

Event 2 start time = 2020-12-23T14:00:00Z

  • events.list() should return event 1 (ended event) since event 1's start time is less than the timeMax.

In your code, when you use timeMin to filter the dates, you are saying that the minimum datetime that the event's end time should be now. Hence, events.list() will return the events that has end time greater than the timeMin(now) which are events that are not yet ended.

Sample:

enter image description here

timeMax = 2020-12-23T11:00:00Z

Response (deleted some unnecessary information):

{
  ....

 "items": [
  {
   "kind": "calendar#event",
   "summary": "ended event 1",
   "start": {
    "dateTime": "2020-12-22T20:30:00+08:00"
   },
   "end": {
    "dateTime": "2020-12-22T21:00:00+08:00"
   }
  },
  {
   "kind": "calendar#event",
   "summary": "ended event 2",
   "start": {
    "dateTime": "2020-12-20T16:00:00+08:00"
   },
   "end": {
    "dateTime": "2020-12-20T16:30:00+08:00"
   }
  },
  {
   "kind": "calendar#event",
   "summary": "ended event 3",
   "start": {
    "dateTime": "2020-12-21T11:00:00+08:00"
   },
   "end": {
    "dateTime": "2020-12-21T11:30:00+08:00"
   }
  }
 ]
}

Upvotes: 1

Related Questions