Error when using Google Calendar v3 API

I'm getting a "Error: Bad Request" error when checking the Google Calendar API.

I have a working setup, and a broken one, although I don't know where the issue is.

My working setup pulls the next 2 events:

msg.payload = { 
    "calendarId": "---@gmail.com", 
    "timeMin": new Date().toISOString(),
    "maxResults": "2",
    "orderBy": "startTime",
    "singleEvents": "true"
}
return msg;

whereas my broken code is trying to pull the next events, for tomorrow, not today.

var tomorrow = new Date(new Date().toISOString());
tomorrow.setDate(new Date().getDate()+1);
console.log(new Date(tomorrow.setHours(0,0,0,0)));

msg.payload = { 
    "calendarId": "---@gmail.com", 
    "timeMin": tomorrow,
    "maxResults": "2",
    "orderBy": "startTime",
    "singleEvents": "true"
}
return msg;

as far as I can see, the messages are the same (aside from the date/time)

this one works

image

this one doesn't

image

any advice on how to fix it?

My guess would be that the debug output converts the date to a similar looking output but in the second it's still a Date object and not a string. On the first one .toISOString() is called but not on second one. So I would suggest trying to change it to have tomorrow.toISOString(). Might be something else of course but that's the only difference I can spot.

I tried the code below, and it still doesn't work.

var tomorrow = new Date();
tomorrow.setDate(new Date().getDate()+1);
console.log(new Date(tomorrow.setHours(0,0,0,0)));
tomorrow.toISOString();

I'll keep playing around (aka fighting with it)

How about:

const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0,0,0,0);

msg.payload = { 
    calendarId: "---@gmail.com", 
    timeMin: tomorrow.toISOString(), 
    maxResults: "2",
    orderBy: "startTime",
    singleEvents: "true"
};

return msg;

This is based on the first Google hit for "javascript date tomorrow": https://flaviocopes.com/how-to-get-tomorrow-date-javascript/

thanks, that gave me what I needed (with a slight change to give timeMax too)

const today = new Date();
const tomorrow = new Date(today);
const nextday = new Date(today);

tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0,0,0,0);

nextday.setDate(nextday.getDate() + 2);
nextday.setHours(0,0,0,0);

msg.payload = { 
    calendarId: "---@gmail.com", 
    timeMin: tomorrow.toISOString(), 
    timeMax: nextday.toISOString(),
    orderBy: "startTime",
    singleEvents: "true"
};

return msg;
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.