How to calculate Google Calendar Event Duration

How can I calculate the Google Calendar event duration. I see the EndTime and StartTime output parameters, but I’m not sure what Javascript to use to figure it out.

Hey @Jason_Jason,

Depending on if you want to use a function node or just an expression there are a couple of different ways.

The theory is use the getTime() method on your dates to get the milliseconds then subtract them and divide the result by the amount of ms in the time unit you are after.

There are a few examples of how to this with JavaScript here: datetime - Difference between dates in JavaScript - Stack Overflow

I can’t remember if the date & time node has an option to work it out but that could be a good enhancement for the future to make this sort of thing easier.

If no one else gets to it before I am near a computer I will get an example together for you.

Thanks, @jon . I wasn’t very clear in my request - I get the theory, but I don’t understand how to put the javascript together to make it happen. When I put this in:
{{$node["Google Calendar"].json["end"]["dateTime"]-$node["Google Calendar"].json["start"]["dateTime"]}}
The result is NaN. (I realize that I didn’t include the necessary divisions to get the right unit).
I think my problem is that I either don’t understand javascript very well, or I don’t understand how n8n uses {{brackets}}.

So with the {{ }} you can put any JavaScript you want although multi line scripts can be tricky, with your dates try adding .getTime() if that doesn’t work it may be better to use a function node to create 2 date objects to work from and you would be able to use any JavaScript you want.

{{$node["Google Calendar"].json["end"]["dateTime"].getTine()-$node["Google Calendar"].json["start"]["dateTime"].getTime()}}

Alright so to get the MS you can do…

{{Date.parse($node["Google Calendar"].json["end"]["dateTime"]) - Date.parse($node["Google Calendar"].json["start"]["dateTime"]}}

Hopefully this helps :+1:


Awesome, thanks so much. I ended up using this:

{{(Date.parse($node[“Google Calendar”].json[“end”][“dateTime”])-Date.parse($node[“Google Calendar”].json[“start”][“dateTime”]))/(60*1000)}}

I really appreciate the support on this forum. You guys are great.