The Javacript date object is a thing of wonder. If you are scheduling events in Apps Script, you may want to do something like figure out when the last friday of each month is.
Here’s how to do it in JavaScript.
And that’s all there is to it. Let’s walk through that.

  • Get the last day of the month required by selecting the month after, then using a date of 0. This will set the date to be one day before the 1st of the next month – in other words – the last day of this month.
  • Adjust the date in the month, by subtracting the day of the week required, from the day of the week of the last day of the month. In case the day of the week of the last day of the month is smaller than the required day of the week, we can just add 7 to it and take the Mod of the final result.

Testing it.

Here’s the last day of the month for every weekday for 2015 & 2016.

Test Code

And here’s how I created that.

For more like this see Google Apps Scripts Snippets
Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.