Search code examples
google-apps-scriptgmail-api

Execute Script daily, only between 6 AM & 6 PM


I have a 32 scripts setup to run every 5 minutes, the scripts use the gmail API so I'm running into some API request limitation issues.

Is there a way to make the scripts run only between a certain time period?

Basically: Every 5 minutes, daily between 6 AM & 6 PM CST.


Solution

  • function runDuringDayTime() {
      var start=new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()).valueOf();
      var sixam=21600000;
      var sixpm=64800000;
      var current=new Date().valueOf();
      var diff=current-start;
      if(diff>sixam && diff<sixpm) {
        //call you function here
      }
    }