A practical field guide from Automation Ace.
How to Get the Last Day of a Month in Zapier Using a Code Step
Finding the last day of a month is a common requirement in billing, reporting, and scheduling workflows — and it is not something Formatter by Zapier handles natively. The last day varies by month (28, 29, 30, or 31 days), and February changes based on leap years. A Code step solves this cleanly in a few lines of JavaScript or Python. This guide provides ready-to-use snippets for the most common scenarios: the last day of the current month, the last day of the month from an input date, and the last day of a specific prior or future month.
Why Formatter by Zapier Cannot Do This Alone
Formatter by Zapier's date tools can add or subtract fixed numbers of days or months from a date, but they cannot determine the last day of a month dynamically. The reason is that "last day of the month" is not a fixed offset — it depends on which month it is. A Code step can call JavaScript's or Python's date APIs, which account for month length and leap years automatically.
For simpler date adjustments that Formatter can handle without a Code step, see adjusting dates and times with Zapier Formatter modifiers.
JavaScript: Last Day of the Current Month
The cleanest JavaScript trick uses day 0 of the next month, which JavaScript's Date constructor interprets as the last day of the current month:
const today = new Date();
const lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0);
const year = lastDay.getFullYear();
const month = String(lastDay.getMonth() + 1).padStart(2, '0');
const day = String(lastDay.getDate()).padStart(2, '0');
output = {
last_day_of_month: `${year}-${month}-${day}`, // e.g. "2025-01-31"
last_day_formatted: lastDay.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }), // e.g. "January 31, 2025"
last_day_number: lastDay.getDate() // e.g. 31
};
JavaScript: Last Day of the Month from an Input Date
When the date comes from a previous Zap step (a trigger field, a form input, or a database record), parse it and apply the same day-0 trick:
// inputData.input_date should be a date string, e.g. "2025-03-15" or "March 15, 2025"
const inputDate = new Date(inputData.input_date);
// Last day of the same month as the input date
const lastDay = new Date(inputDate.getFullYear(), inputDate.getMonth() + 1, 0);
const year = lastDay.getFullYear();
const month = String(lastDay.getMonth() + 1).padStart(2, '0');
const day = String(lastDay.getDate()).padStart(2, '0');
output = {
input_date: inputData.input_date,
last_day_of_month: `${year}-${month}-${day}`,
last_day_number: lastDay.getDate(),
is_leap_year: (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0))
};
Pass the input date string as input_date in the Code step's input data fields, mapped from the previous Zap step that provides the date.
JavaScript: Last Day of a Specific Prior Month
For billing workflows that need the last day of the previous month (a common report period boundary):
const today = new Date();
// Last day of the previous month: day 0 of the current month
const lastDay = new Date(today.getFullYear(), today.getMonth(), 0);
const year = lastDay.getFullYear();
const month = String(lastDay.getMonth() + 1).padStart(2, '0');
const day = String(lastDay.getDate()).padStart(2, '0');
output = {
last_day_of_previous_month: `${year}-${month}-${day}`,
month_name: lastDay.toLocaleDateString('en-US', { month: 'long' }),
year: String(year)
};
Python: Last Day of the Month from an Input Date
For Python Code steps, the calendar module provides monthrange() which returns the number of days in any month, accounting for leap years:
import calendar
from datetime import datetime, date
# Parse the input date
input_date = datetime.strptime(input_data['input_date'], '%Y-%m-%d').date()
# Get the last day of that month
last_day_num = calendar.monthrange(input_date.year, input_date.month)[1]
last_day = date(input_date.year, input_date.month, last_day_num)
return {
'last_day_of_month': last_day.strftime('%Y-%m-%d'),
'last_day_formatted': last_day.strftime('%B %d, %Y'),
'last_day_number': str(last_day_num),
'days_remaining': str((last_day - input_date).days)
}
The calendar module is part of Python's standard library and is available in Zapier Code steps. For a full reference of available Python modules, see Python libraries supported in Zapier Code steps.
Practical Use Cases
- Billing cycle end dates: A subscription management Zap determines the last day of the current billing period to set a renewal reminder or invoice due date
- Monthly reporting windows: A reporting Zap triggered on the 1st of each month calculates the last day of the prior month to define the reporting period
- Deadline calculations: A project management Zap sets a deadline for end-of-month tasks regardless of which month it is running in
- Calendar events: A Google Calendar event creation Zap sets the end date of a monthly recurring event to the last day of each month
- Days remaining: The Python snippet's
days_remainingoutput counts how many days are left in the month from the input date — useful for urgency-based routing
The day-0 trick in JavaScript — creating a date with day 0 of month N+1 — is one of those patterns worth memorizing. It reliably returns the last day of month N regardless of month length or leap year, with no lookup tables or conditional logic needed. One line of date math, any month, any year.
For more Code step patterns and what is available in each environment, see JavaScript libraries in Zapier Code steps and Python libraries in Zapier Code steps. For help building a date-dependent automation workflow, talk to Automation Ace.
Disclaimer: This article may include links to apps, products, or services. Some links may be affiliate links, which means Automation Ace may earn a commission at no extra cost to you.