# Get Events from Google Calendar with "minTime" and "maxTime"

**URL:** <https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771>\
**Category:** General\
**Created:** [13 April 2020 17:03 UTC](https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771 "2020-04-13T17:03:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstebe](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@lstebe](https://discourse.nodered.org/u/lstebe)\
**Post date:** [13 April 2020 17:03 UTC](https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771/1 "2020-04-13T17:03:51Z")

</div>

Hi at all,

I ran into an Issue with getting Events via the node-red-contrib-google Node.  
It´s all set up well so far and works, but with one twist, here's the setup:

I have a global object called systime were i store some date data to reuse it for different applications.  
it looks like this:  
 ![systime](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/1/0162848486c0fb78068a47a5c15d28c767d98734.jpeg)

As you can see there are two variables set "nowIso" and "inaweek" which are perfectly formatted as ISO date string as required by the Google API.

Now i trigger a function node with an Inject to perform an API request. like this:

```auto
systime = global.get("systime");
now = systime.nowIso;
inaweek = systime.inaweek;

msg.time = {now: now, inaweek:inaweek};
msg.payload = {calendarId:"mycalendarID@googlemail.com", maxResults: "20"};
msg.payload.timeMin = now;
msg.payload.timeMax = inaweek;
return msg;

```

I use the msg.time for debugging purpose and see, that in msg.time i have the same needed ISO date strings.

but I do get following error from the Google node:

```auto
"Error: Bad Request"

```

.....  
When passing the ISO date directly like:

```auto
systime = global.get("systime");
now = systime.nowIso;
inaweek = systime.inaweek;

msg.time = {now: now, inaweek:inaweek};
msg.payload = {calendarId:"mycalendarID@googlemail.com", maxResults: "20"};
msg.payload.timeMin = "2020-04-13T16:31:21.649Z";
msg.payload.timeMax = "2020-04-20T16:31:21.649Z";
return msg;

```

it works totally fine and I totally can't figure out why!  
please help an obvious blockhead...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 April 2020 20:28 UTC](https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771/2 "2020-04-13T20:28:56Z")

</div>

Are you sure that the time values stored in context are strings and not Date objects (which the debug panel helpfully converts to visible ISO strings for you).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 April 2020 20:31 UTC](https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771/3 "2020-04-13T20:31:25Z")

</div>

> [@lstebe](#):
>
> msg.payload.timeMin = now;  
> msg.payload.timeMax = inaweek;

To check my hypothesis try

```auto
msg.payload.timeMin = now.toISOString();
msg.payload.timeMax = inaweek.toISOString();

```

---

<div class="post-metadata">

**Author:** ![lstebe](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@lstebe](https://discourse.nodered.org/u/lstebe)\
**Post date:** [13 April 2020 21:50 UTC](https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771/4 "2020-04-13T21:50:52Z")

</div>

thank you for your fast reponse!  
I absolutely am!  
the string in the screenshot above is a sting, other wise it would appear in blue.  
and typeof returns string as well.

---

<div class="post-metadata">

**Author:** ![lstebe](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@lstebe](https://discourse.nodered.org/u/lstebe)\
**Post date:** [13 April 2020 22:08 UTC](https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771/5 "2020-04-13T22:08:10Z")

</div>

Okay with your guess I however solved it, even if I tried it quite like that before.  
I had to set inaweek to a timestamp.  
when doing this in my function:

```auto
systime = global.get("systime");
now = systime.nowIso;
inaweek = systime.inaweek;
inaweek = new Date(inaweek);

msg.time = {now: now.toISOString() , inaweek:inaweek};
msg.payload = {calendarId:"myCalendarID", maxResults: "20"};
msg.payload.timeMin = now.toISOString();
msg.payload.timeMax = inaweek.toISOString();
return msg;

```

it works! 🤦‍♂️

pretty weird, it works with the ISO String of now, but not with the String from inaweek, that i formatted the exact same way?! is this, because it is a date in the future? are there any restrictions by JS, Node.js, NR?  
e.g.

```auto
//today is 13.04.2020
// this works
now = "2020-04-13T21:55:20.119Z";
now = now.toISOString(); //even if this obviously is an ISO

//this doesnt work
inaweek = "2020-04-20T21:55:20.119Z";
inaweek = inaweek.toISOString();
// this returns
// "Error: inaweek.toISOString is not a function"

```

anyway, i will have learned to generate a new dateobject everytime i work with future dates!  
and thank you very much Colin, you pushed me over the edge 😀

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 April 2020 07:25 UTC](https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771/6 "2020-04-14T07:25:26Z")

</div>

> [@lstebe](#):
>
> msg.payload.timeMin = now.toISOString();

If `systime.nowISO` is a string, as I think you are saying then the above would fail because toISOString() is not a function you can call on a string.

---

<div class="post-metadata">

**Author:** ![lstebe](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@lstebe](https://discourse.nodered.org/u/lstebe)\
**Post date:** [14 April 2020 08:20 UTC](https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771/7 "2020-04-14T08:20:31Z")

</div>

thank you again, colin. there seemed to be a mistake of mine that i overlooked.  
the solution is the following:

storing the ISO strings as before in my systime function,  
comments for potential future Datefunction victims.

```auto
time = new Date(); // new date object

hour = time.getHours(); // extract the stuff
minute = time.getMinutes();
day = time.getDay();
date = time.getDate();
month = time.getMonth()+1; // necessary because months are indexed 0-11
year = time.getFullYear();

nowIso = time.toISOString(); // convert now to ISO string
inaweekstamp = Date.now()+604800000; // for inaweek timestamp + 7days in millisecs 
inaweek = new Date(inaweekstamp).toISOString(); // new dateobject from that and convert to ISO

var showhour;
var showminute;

if(minute < 10){
    showminute = "0"+minute;
}else{showminute = minute}
if(hour < 10){
    showhour = "0"+hour;
}else{showhour = hour}

global.set("systime",{"hour":hour,"minute":minute, "day":day, "date":date, "month": month, "year":year,"showhour":showhour, "showminute":showminute, "nowIso":nowIso,inaweek:inaweek});
node.status({text:"System Time: "+showhour+":"+showminute})

```

where now is safe, that nowISO and inaweek are ISO strings without a doubt

recall the variables anywhere else

```auto
systime = global.get("systime");
now = systime.nowIso;
inaweek = systime.inaweek;

msg.time = {now: now, inaweek:inaweek};
msg.payload = {calendarId:"stebe41@googlemail.com", maxResults: "20"};
msg.payload.timeMin = now;
msg.payload.timeMax = inaweek;
return msg;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 April 2020 09:25 UTC](https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771/8 "2020-04-14T09:25:19Z")

</div>

Personally I would do it the other way round, keep everything as Date objects (so you can do things like add a week or convert to local time or whatever) and only convert to a string when you need to, which won't be very often.

---

<div class="post-metadata">

**Author:** ![lstebe](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@lstebe](https://discourse.nodered.org/u/lstebe)\
**Post date:** [14 April 2020 10:31 UTC](https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771/9 "2020-04-14T10:31:32Z")

</div>

You are right in most cases. In fact I want to look up multiple Google cals which requests are triggered separately, so in my particular case, I want do do the toISOString() once per period centralized!  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 April 2020 10:31 UTC](https://discourse.nodered.org/t/get-events-from-google-calendar-with-mintime-and-maxtime/24771/10 "2020-04-28T10:31:37Z")

</div>

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