简体   繁体   中英

How to convert and pass the date in GAS to create an event in Google calendar?

This is how we create the event in GAS: https://developers.google.com/apps-script/reference/calendar/calendar#createEvent(String,Date,Date,Object)

var event = CalendarApp.getDefaultCalendar().createEvent('Apollo 11 Landing',
     new Date('July 20, 1969 20:00:00 UTC'),
     new Date('July 20, 1969 21:00:00 UTC'),
     {location: 'The Moon'});
 Logger.log('Event ID: ' + event.getId());

Here the input date is in format (E, dd MMM yyyy HH:mm:ss Z) ..

I am getting the output as separate date and time

var bookTime, bookDate; // bookDate = 2014-11-26 and bookTime = 09    
var startDateTime  = bookDate + " " + bookTime+ ":00:00"; //startDateTime = 2014-11-26 09:00:00

How can I use this time to create and event,

Any Leads?

Since it seems your starting values are strings, the easiest way is to parse the values and create a new date object with the right parameters using string methods. Below is an example code with the data you provided :

function testDate(){
  var date = convertToDate("2014-11-26", "09");
  Logger.log(date);
}

function convertToDate(dateString,timeString){
  var dateData = dateString.split("-");
  var hour = Number(timeString);
  var date = new Date(new Date().setFullYear(dateData[0],dateData[1]-1,dateData[2])).setHours(hour,0,0,0);
  return new Date(date);
}

The date object is directly useable in the calendar event creation method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM