user3316048
user3316048

Reputation: 33

Run trigger in partiuclar time interval only

I use this part of the code (and also time trigger) to run script every 5 minutes on "working hours" but now I need some adjustments

function officeHours(){
var nowH=new Date().getHours();
var nowD=new Date().getDay();
Logger.log('day : '+nowD+'   Hours : '+nowH)
if(nowH>17||nowH<8||nowD==6||nowD==0){return}
Browser.msgBox('time to work !');//normally your real function should begin here...
}

Reference: Working hours only trigger

But how be more specific? e.g. set working hours from 8:30 to 17:30 or another e.g. when shift (working hours) starts at one day and ends next day (from 22:30 at day one to 06:30 at next day)?

Upvotes: 1

Views: 98

Answers (1)

Amit Agarwal
Amit Agarwal

Reputation: 11268

You can check if the current hour is between the shift hours as in this example.

function officeHours(startShift, endShift){
  startShift = startShift || 23;
  endShift = endShift || 7;
  var currentHour = new Date().getHours();
  if (startShift > endShift) {
    if (currentHour >= startShift || currentHour <= endShift) {
      // run the trigger function
    } 
  } else {
    if (currentHour >= startShift && currentHour <= endShift) {
      // run the trigger function
    } 
  }
}

Upvotes: 3

Related Questions