1

I want to run a trigger at specific date and time (+or - 15 min).

I have a date min string with YYYY-MM-DD HH:MM format say 2017-04-01 18:05 How will I modify the below script to run at 18 hours and 5 minutes.

 var triggerDay = new Date(2017, 04, 01);
 ScriptApp.newTrigger("myFunction")
   .timeBased()
   .at(triggerDay)
   .create();
Code Guy
  • 3,059
  • 2
  • 30
  • 74

2 Answers2

2

You can try this:

function createTimeDrivenTriggers() {
// Trigger on 2017-04-01 around 18:00 (±15 min).
ScriptApp.newTrigger('myFunction')
    .timeBased()
    .atDate(2017, 04, 01)
    .atHour(18)
    .create();
}

https://developers.google.com/apps-script/reference/script/clock-trigger-builder

WEC
  • 21
  • 1
  • You might add .nearMinute(5) but since your +- 15 minutes you'll probably not be able to determine if it's actually working. – Cooper Apr 01 '17 at 14:42
  • How about the usage of after(durationMilliseconds), what does it actually mean? It i the number of milli we have to specify since epoch? – Code Guy Apr 01 '17 at 14:45
1
// the full Date constructor is:
// new Date( year, month, day, hour, min, sec, ms )
var triggerDay = new Date(2017, 3, 01, 18, 5);
ScriptApp.newTrigger("myFunction")
  .timeBased()
  .at(triggerDay)
  .create();

I'm pretty sure that the month in the constructor is zero-based (January==0), so, I changed the 04 to a 3.

HardScale
  • 971
  • 1
  • 7
  • 18