0
votes

So I am currently using the Google Calendar API, specifically the freebusy query (as seen here https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query).

The request body requires a 'timeMin' and 'timeMax'. And here comes my question...

How would I set these two values dynamically based off the current datetime. Basically use the current datetime to set an interval, say an hour before now, and an hour after now.

I have seen other stackoverflow posts (Subtracting hours from date string) on how to setHours by getHours but the problem with this method seems to be that it alters the current time instead of creating a new instance.

Also I need to keep the resulting min and max datetimes in ISOstring format (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) because this is what is used in Google Calendar API request body.

Chrome DevTools Console

1

1 Answers

2
votes

I believe you're looking for something like this:

var now = new Date();
var msNow = now.getTime();  //  total mil1iseconds since 1970/01/01
var timeMax = new Date(msNow + 60 * 60 * 1000); //  "now" plus one hour
var timeMin = new Date(msNow - 60 * 60 * 1000); //  "now" minus one hour
console.log(now);
console.log(timeMin);
console.log(timeMax);