Using the Calendar Node --> Resource: Event --> Operation: Get Many gives old data.
I want to “get” all calendar events due in the current day (24 hours).
I tried doing this with the Get Many option and limiting it to 50 then sorting by date. But all of the events were over a year old…
It looks like the Get Many option with a limit of 50 provides the OLDEST 50 events.
I do NOT want to get all the data and parse it after the fact because that would take a long time (there are many thousands of events since the beginning of the calendar and trying to put this into a code node throws an Error).
Error message
Problem running workflow Request failed with status code 413
How could I get the calendar events due to arise for a given date?
Hi @hdotking, the Google calendar node provides options that you can use. E.g. you might wanna use the Order by option to sort your events from newest to oldes or use the Start/End time option. Have tried to use any of those?
Yes i’ve tried playing with all of the existing options.
When I try setting Start Time to last week it still shows me emails from 2021. End Time does the same. Using them both returns no events. (see image below for Start Time)
The start/end options are the wrong way around and should be renamed. This is what they actually do:
Start time → Before (at least some part of the event must be before this time)
End time → After (at least some part of the event must be after this time)
Note that if you have recurring events, they will be returned but with the date of the original occurrence, not the occurrence that happens within your window.
Here’s a workflow that fixes this, and also corrects for changes in daylight savings time. (It may not work on n8n versions earlier than 0.216 because of some internal changes we made)
Sure thing. PS: for some reason your code snippet resets the timezone in the timestamp returned to -05.
I fixed it by hardcoding my desired timeZone and using it in the setCurrentDate function.
Here’s the updated code that respects the user’s timezone. It turns out I just needed to add this line of code before .toISO newDate = newDate.setZone(zoneName)
// Recurring meetings seem to give the original event's start/end, not the current event's
// We get around this by setting the start/end date to the current date (but keeping the original time)
// Obviously this is a bit of a hack and would be better handled in the Google Calendar node itself
// We also need to account for differences in whether we're in DST between the original event and the current time
function setCurrentDate(date) {
/*var newDate = new Date(date);
newDate.setDate(new Date().getDate());
newDate.setMonth(new Date().getMonth());
newDate.setFullYear(new Date().getFullYear());*/
var zoneName = "Europe/London"
var newDate = DateTime.fromISO(date);
newDate = newDate.set({
day: $now.day,
month: $now.month,
year: $now.year
});
newDate = newDate.setZone(zoneName)
return newDate.toISO();
}
// If we're currently in DST and this date isn't, add an hour to it
// If we're not currently in DST and this date is, subtract an hour from it
// To calculate DST we use the timezone that the event is in
function normaliseDST(date, zoneName) {
var newDate = DateTime.fromISO(date).setZone(zoneName);
var now = DateTime.now().setZone(zoneName);
console.log("start", newDate, newDate.isInDST);
console.log("now", now, now.isInDST);
if(newDate.isInDST && !now.isInDST) {
console.log("Normalising +1")
newDate = newDate.plus({hour: 1});
} else if(!newDate.isInDST && now.isInDST) {
console.log("Normalising -1")
newDate = newDate.minus({hour: 1});
}
console.log("adjusted", newDate, newDate.isInDST);
newDate = newDate.setZone(zoneName)
return newDate.toISO();
}
// Check we haven't been passed an empty item
if (Object.keys(item).length == 0) return out;
// Get the current time in the time zone of the event
/*console.log("startString", item.start.dateTime);
var start = DateTime.fromISO(item.start.dateTime).setZone(item.start.timeZone);
console.log("start", start);
console.log("startisdst", start.isInDST);
console.log("zoneName", start.zoneName);
var now = $now.setZone(start.zoneName);
console.log("now", now);
console.log("nowisdst", now.isInDST);*/
if('start' in item) {
item.calcStartNormalised = setCurrentDate(normaliseDST(item.start.dateTime, item.start.timeZone)); // What we used to do, but not needed any more?
item.calcStart = setCurrentDate(item.start.dateTime, item.start.timeZone);
//item.calcEnd = setCurrentDate(item.end.dateTime);
console.log(item.start.timeZone)
}
return item;