Sam Stern
Sam Stern

Reputation: 25134

Google Calendar API querying all-day events

I have an app that uses the Calendar v3 API to create, query, and modify all-day events on Google Calendar. These events look something like this when I create them:

{ 
  summary: 'My Event",
  start: { date: '2014-07-26' },
  end: { date: '2014-07-26' } 
}

So if I want to query all of such events between July 20th and July 26th, I send a query like this:

GET www.googleapis.com/calendar/v3/{calendarID}/events?timeMin=2014-07-20T00%3A00%3A00.000Z&timeMax=2014-07-26T00%3A00%3A00.000Z

In a more readable format, the parameters are:

timeMin:2014-07-20T00:00:00.000Z
timeMax:2014-07-26T00:00:00.000Z

However, this excludes all of the events with the date 2014-07-26, it gets only the events for the 20th to the 25th. In my parameters you can see that I have used setUTCHours(0) in order to have no time zone information.

If I remove the calls to setUTCHours(0) for the timeMin and timeMax parameters then I have the opposite problem, I get 7/21 through 7/26 and miss the events on 7/20. How can I reliably get all of the all-day events for the week in any time zone?

Upvotes: 4

Views: 6149

Answers (3)

Sam Stern
Sam Stern

Reputation: 25134

The problem was with how I was creating events. To create an all-day event on date 1, you should set the start to x and the end to x + 1 day.

  var dateStringStart = dateToString(dateObj);
  var dateObjEnd = new Date(dateObj.getTime() + (24 * 60 * 60 * 1000));
  var dateStringEnd = dateToString(dateObjEnd);

  gapi.client.request({
    path: '/calendar/v3/calendars/' + id + '/events',
    method: 'POST',
    body: {
      summary: name,
      start: {
        date: dateStringStart
      },
      end: {
        date: dateStringEnd
      }
    },
    callback: function(eventObj) {
      // ...
    }
  });

Upvotes: 1

Eric G
Eric G

Reputation: 1292

Just came across this myself. For filtering, the end time is exclusive while the start time is inclusive. See the events spec here. Presumably when you just specify a date and not a time, it treats it something like 00:00:00 UTC -- so not inclusive of that day. But that's just speculation on my part. Anyway, if you +1 to your end date it should work the way you expect it to.

Upvotes: 4

luc
luc

Reputation: 3782

I would ask for +1 day on both sides of the week and post-filter the events that are not interesting.

Upvotes: 2

Related Questions