简体   繁体   English

如何在GAS中转换和传递日期以在Google日历中创建事件?

[英]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) 这是我们在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) .. 这里的输入日期格式为(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. 由于您的起始值似乎是字符串,所以最简单的方法是解析值并使用字符串方法使用正确的参数创建新的date对象。 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. 日期对象可直接用于日历事件创建方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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