Jasmine
Jasmine

Reputation: 15

How can I get the start and end time of a calendar event in Google Apps Script?

The getStartTime() and getEndTime() is only returning the date. How can I get the start and end time of the event?

var allEvents = calendar.getEvents(new Date('June 3, 2020'), new Date('August 6, 2020'));

var startDateTime = allEvents[j].getStartTime();

var endDateTime = allEvents[j].getEndTime(); 

Upvotes: 0

Views: 1642

Answers (1)

Marios
Marios

Reputation: 27360

startDateTime and endDateTime are Date objects, meaning you can apply all the relevant methods.

var allEvents = calendar.getEvents(new Date('June 3, 2020'), new Date('August 6, 2020'));

var startDateTime = allEvents[j].getStartTime();
var startDateTime24 = startDateTime.getHours() + ":" + startDateTime.getMinutes()

var endDateTime = allEvents[j].getEndTime(); 
var endDateTime24 = endDateTime.getHours() + ":" + endDateTime.getMinutes()

Now startDateTime24 and endDateTime24 contain the time of the start and end of the event respectively in the 24-hour notation in the form hh:mm.


Note in the code I assume that calendar holds a reference to an instance of CalendarApp.

Upvotes: 1

Related Questions