0

I get the error message (below) when I run the code. My intention is to create a trigger at exact time 2017-04-03 20:10

ScriptApp.newTrigger('myfunction').timeBased().atDate(2017,04,03).atHour(20).nearMinute(10).create();

Log

[17-04-02 20:16:53:455 IST] TriggerBuilder.timeBased() [0 seconds]
[17-04-02 20:16:53:456 IST] ClockTriggerBuilder.atDate([2017, 4, 3]) [0 seconds]
[17-04-02 20:16:53:456 IST] ClockTriggerBuilder.atHour([20]) [0 seconds]
[17-04-02 20:16:53:461 IST] Execution failed: Error: Already chosen a specific date time with at() or atDate(). (line 109, file "Code") [11.583 seconds total runtime]
Code Guy
  • 3,059
  • 2
  • 30
  • 74
  • I found this [link](http://stackoverflow.com/questions/30561061/trigger-every-14-days-starting-on-a-specific-date). I guess you can't use multiple at clauses. – Cooper Apr 02 '17 at 15:10
  • That means we can't create a trigger of type date with time. If I specifiy date then it means it will be triggered at midnight? – Code Guy Apr 02 '17 at 15:29
  • I guess you could create a trigger that runs another script that creates another trigger. But yeh I'm hoping Sandy is wrong about that but I kind of doubt it. – Cooper Apr 02 '17 at 15:33
  • But it is possible from script editor isn't it? Oncetimer – Code Guy Apr 02 '17 at 15:35
  • From the UI in the script editor it looks you can use a datetime object. – Cooper Apr 02 '17 at 15:38

1 Answers1

2

You can create a new date object with a specific date and time then use new trigger().At(date), not to be confused with atDate(), function to create trigger at that date and time.

var dt = new Date(2017,03,03,20,10)
//Month index starts from 0 i.e. Jan =0,Feb=1... So on
 ScriptApp.newTrigger('myFunction').timeBased().at(dt).create()

Hope that helps!

Jack Brown
  • 5,802
  • 2
  • 12
  • 27
  • Thanks a lot @Jack Brown. If I want to programically know whether the trigger is created without errors how will I?Coz if I give date as 2017,02,31,20,17 trig will be created with uniq id but it actually doesn't as the date is wrong. – Code Guy Apr 03 '17 at 05:37
  • Actually (2017,02,31,20,17) is march 31, which is a valid date. For Feb you would do this new Date(2017,01,31,20,17) which will be automatically set to the following date of 2017-03-03 20:17. You can read about it [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Parameters). You will have to programmatically determine if the values you entered in the date function match what was created using getFullYear(), getMonth(), getDay() etc. – Jack Brown Apr 03 '17 at 07:40