What happens when you set a Month Timer Trigger to fire on the 31st for a month that doesn't have...
Categories:
Google Apps Script: Month Timer Triggers on the 31st

Explore the behavior of Google Apps Script time-driven triggers set for the 31st day of the month when that month has fewer than 31 days.
Google Apps Script provides powerful time-driven triggers that allow scripts to run automatically at specified intervals. One common trigger type is the 'Month timer', which can be configured to fire on a specific day of the month. However, a frequent point of confusion arises when a trigger is set for the 31st day of the month. What happens in months like February, April, June, September, or November, which do not have 31 days?
Understanding Apps Script Timer Trigger Behavior
When you configure a time-driven trigger in Google Apps Script to run on the 31st day of the month, the system intelligently handles months with fewer than 31 days. Instead of failing or skipping the month entirely, the trigger will execute on the last day of that particular month. This ensures that your script still runs, even if the exact date (the 31st) doesn't exist in a given month.
flowchart TD A[Trigger Set for 31st of Month] --> B{Current Month Has 31 Days?} B -->|Yes| C[Trigger Fires on 31st] B -->|No| D{Current Month Has < 31 Days?} D -->|Yes| E[Trigger Fires on Last Day of Month] E --> F[Script Executes] C --> F
Flowchart illustrating Google Apps Script monthly trigger logic
Practical Implications and Use Cases
This behavior is generally beneficial, as it prevents scripts from being missed in shorter months. For example, if you have a script that generates a monthly report on the 'last day of the month', setting the trigger to the 31st will achieve this goal consistently, regardless of whether the month ends on the 28th, 29th, 30th, or 31st. This simplifies trigger management, as you don't need to create separate triggers for different month lengths.
Verifying Trigger Execution
To confirm this behavior, you can set up a simple script and a monthly trigger. The script can log the execution date to a spreadsheet or to the script's execution log. Observe the logs over several months, including those with 30 days (e.g., April) and 28/29 days (February), to see the trigger firing on the last day.
function logMonthlyTriggerExecution() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('TriggerLogs');
if (!sheet) {
SpreadsheetApp.getActiveSpreadsheet().insertSheet('TriggerLogs');
}
const now = new Date();
sheet.appendRow([now.toLocaleString(), 'Monthly trigger executed']);
Logger.log('Monthly trigger executed on: ' + now.toLocaleString());
}
A simple Google Apps Script function to log trigger execution dates.
1. Create the Script
Open a Google Sheet, go to Extensions > Apps Script, and paste the logMonthlyTriggerExecution
function into Code.gs
.
2. Set Up the Trigger
In the Apps Script editor, click the 'Triggers' icon (alarm clock). Click 'Add Trigger'. For 'Choose which function to run', select logMonthlyTriggerExecution
. For 'Select event source', choose 'Time-driven'. For 'Select type of time based trigger', choose 'Month timer'. For 'Select day of the month', choose '31st day'. For 'Select time of day', choose a convenient time. Save the trigger.
3. Monitor Execution
Over the next few months, observe the 'TriggerLogs' sheet in your spreadsheet. You will see entries for the 31st in longer months and for the last day (e.g., 30th for April, 28th/29th for February) in shorter months.