1
votes

In trying to extract a day agenda from Google Calendars as part of a workflow process administered using Google Apps Script I call getEventsForDay() from the CalendarApp service.

A specific (simplified) example:

var calendarView = CalendarApp.getCalendarById(ScriptProperties.getProperty("calendarID"));
var today = new Date();
var todaysEvents = calendarView.getEventsForDay(today);

This populates the array todaysEvents with all the days events fine, but also includes recurring events prior to and post where the recurrence spans the date in the query but does not occur on the day itself. (query Wed, recurring event on Mon, Tue, Fri will be returned)

We run our calendars to GMT/BST so I have looked into whether this is a function of Date() returning dates as PDT and not UTC, but on occasion events that are over 24 hours in either direction are returned.

Assuming this is not a bug, I can't work out a good way of filtering out these 'invalid' events from the returned array. Can you?

Or is this a bug?

Thanks

1

1 Answers

3
votes

Jonathan,

We're figuring it out. Internally, an all day event is represented in UTC, so this will bleed over into either the previous day or the following day depending on what time zone you're in. Someone asked me a question about this a week ago, and I proposed the following workaround while we sort this out:

var startOfDay = new Date();
startOfDay.setUTCHours(0);
startOfDay.setMinutes(0);
startOfDay.setSeconds(0);
startOfDay.setMilliseconds(0);  
var endOfDay = new Date(startOfDay.getTime() + 24 * 60 * 60 * 1000);

var events = CalendarApp.getDefaultCalendar().getEvents(startOfDay, endOfDay)

I realize that this isn't the best experience right now, but it should do what you need. You can abstract this into a function.