首页 > 解决方案 > 如何在 Google Apps 脚本中获取日历活动的开始和结束时间?

问题描述

getStartTime()andgetEndTime()只返回日期。如何获取活动的开始和结束时间?

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

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

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

标签: google-apps-scriptgoogle-calendar-api

解决方案


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.


推荐阅读