2

In google scripts I know there are triggers to run by date, but I don't think that will work because month's have different amounts of days. So I was wondering if there's a way to set a trigger to run at 11PM on the last night of each month, no matter if that's a 30 or a 31.

Thanks

bkoenig
  • 45
  • 7
  • 1
    Is [this](http://stackoverflow.com/questions/222309/calculate-last-day-of-month-in-javascript) kind of what you're looking for? – Samuel Hill Jan 07 '16 at 13:57

1 Answers1

5

First create a trigger from project Edit > current project's trigger or register it programmatically that will run every day at 11 pm.

ScriptApp.newTrigger("myTriggerFunction")
   .timeBased()
   .atHour(23)
   .everyDays(1) 
   .create();

Then in your trigger handler, check if today is the last day of the month, then do your work.

function myTriggrFunction()
{
  var today = new Date();
  var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);

  if(today.getDate() == lastDayOfMonth.getDate() )
  {
    // your work to be done
  }
}
iJay
  • 4,205
  • 5
  • 35
  • 63