简体   繁体   中英

How can I add one day the start date of an event in Google Apps Script

I have a google apps script that gets the start and end time of a not all-day calendar event. The event goes from 8:00 to 9:00 on the 16th and I wat a script that will essentially duplicate it onto the 17th, then the 18th and so on. I know you are probably thinking that I should just make the event repeating every day but I am using a program that ignores repeating events.

Here's what I have so far:

function myFunction() {
var cal = CalendarApp.getCalendarById('1234@calendar.google.com')
var spanish = cal.getEventById('12345')

var title = spanish.getTitle()
var desc = spanish.getDescription()
var start = spanish.getStartTime()
var end = spanish.getEndTime()
cal.createEvent(title, START TIME + 1 DAY, END TIME + 1 DAY)
}

Any advice is appreciated

The most reliable way to add a day is by using milliseconds. Adding 1 to the day will work most of the time, but if the event was on the last day of the year, and you want to move the event one day, then both the year and month will be different. So, getting the year and the month and reusing it, wouldn't work in that case.

Also, if the original event was on the last day of the month, then adding one day will put the changed event into a new month, and so reusing the month of the last event for the date won't work in that situation.

function testMoveDateByOneDay() {
  var endDate,foundEvent, startDate;

  var cal = CalendarApp.getDefaultCalendar();

  startDate = new Date();
  endDate = new Date();
  endDate = endDate.setDate(endDate.getDate() + 1);

  var event = cal.createAllDayEvent('Test Event', startDate, endDate)

  var eventID = event.getId();

  var foundEvent = cal.getEventById(eventID);

  var title = foundEvent.getTitle()
  var desc = foundEvent.getDescription()

  var start = foundEvent.getStartTime();
  var end = foundEvent.getEndTime();

  //Logger.log(typeof start)

  var startInMS = start.getTime() + 86400000;//Add one day in milliseconds
  var endInMS = end.getTime() + 86400000;

  start = new Date(startInMS);
  end = new Date(endInMS);

  //Logger.log(typeof start)

  event.setTime(start, end);//Change the day of  of this event
}

try:

var newStart=new Date(start.getFullYear(),start.getMonth(),start.getDate()+1);

You can add more terms if you want time.

JavaScript Date()

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