Get First Day of Previous Month
In this article
To get the first day of the previous month, take the current date, subtract one month, and then set the day to 1. For example, if today is March 15, the first day of the previous month is February 1. This simple logic works in most programming languages, spreadsheet formulas, and database queries, though the exact syntax varies.
in t | Current date (YYYY-MM-DD) | - | 2025-03-15 | 0 out first_prev | First day of previous month | - | date(t_year, t_month - 1, 1) | 0
Formula
The general formula is: First day of previous month = date( year(current_date), month(current_date) - 1, 1 )
Where:
- current_date – the date you are starting from (e.g., 2025-03-15)
- year() – extracts the year (e.g., 2025)
- month() – extracts the month number (e.g., 3 for March)
- date(y, m, d) – constructs a date from year, month, and day components
When you subtract 1 from the month, the date function automatically handles the year rollover. For example, if the current month is January (month 1), subtracting 1 gives month 0, which the function interprets as December of the previous year.
Worked Example
Using the default date of 2025-03-15:
- Extract the year: 2025
- Extract the month: 3
- Subtract 1 from the month: 3 – 1 = 2
- Set the day to 1
- Result: 2025-02-01
If the date were 2025-01-10:
- Year: 2025
- Month: 1
- 1 – 1 = 0 → interpreted as December of the previous year
- Result: 2024-12-01
Why This Is Useful
Getting the first day of the previous month is a common need in reporting, data analysis, and financial calculations. You might want to:
- Filter records from the entire previous month (e.g., sales or log entries)
- Calculate a date range for monthly billing or subscription cycles
- Generate monthly summaries or dashboards that always show the last complete month
- Schedule recurring jobs that run at the start of each month
Because the day is always set to 1, you avoid the problem of months having different lengths (28, 30, or 31 days).
How to Do It in Common Tools
Here are the exact expressions for popular platforms:
- Excel / Google Sheets:
=EOMONTH(TODAY(),-2)+1– this returns the last day of the month two months back, then adds 1 day to get the first of the previous month. - Python (datetime):
date.today().replace(day=1) - timedelta(days=1)then take the first of that month, or more directly:(date.today().replace(day=1) - timedelta(days=1)).replace(day=1) - JavaScript:
new Date(now.getFullYear(), now.getMonth()-1, 1) - SQL (MySQL):
SELECT DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, '%Y-%m-01') - SQL (PostgreSQL):
SELECT date_trunc('month', CURRENT_DATE - INTERVAL '1 month') - Power BI / DAX:
STARTOFMONTH(EDATE(TODAY(), -1))
Each of these handles the year wrap automatically.
Common Mistakes and Pitfalls
People often make these errors when trying to get the first day of the previous month:
- Subtracting 30 days instead of 1 month. This can land you in the wrong month if the current month has 31 days. For example, March 30 minus 30 days is February 28 – correct, but March 31 minus 30 days is March 1, not February 1.
- Using the last day of the current month. Some try
EOMONTH(TODAY(),-1)which gives the last day of the previous month, not the first. You need to add 1 day to that. - Forgetting the year rollover. In January, subtracting 1 month from month 1 gives month 0, which many naive implementations treat as an error. Always use a date library that handles month 0 as December of the previous year.
- Hardcoding the day as 1 after subtracting days. If you subtract 30 days from March 15, you get February 13 – that’s not the first of the month.
Always test with a date in January to ensure the year changes correctly.
Alternatives and Edge Cases
If you cannot use a date library, you can compute the first day of the previous month manually:
- Get the current year and month.
- If month = 1, set year = year – 1 and month = 12.
- Otherwise, month = month – 1.
- Set day = 1.
This is essentially what the formula above does. Edge cases to consider:
- Leap years: February always has 28 or 29 days, but since we set day to 1, this doesn’t affect the result.
- Time zones: If you are working with UTC and local dates, ensure you use the correct date reference. The “current date” can differ by a day depending on time zone.
- Date-only vs. datetime: Some systems include a time component. You may need to strip the time or set it to midnight.
Frequently Asked Questions
How do I get the first day of the previous month in Excel?
Use =EOMONTH(TODAY(),-2)+1. This finds the last day of the month two months back, then adds one day, giving you the first day of the previous month. For example, if today is March 15, it returns February 1.
What if today is January? Does the year change?
Yes. If today is January 10, the first day of the previous month is December 1 of the previous year. The formula date(year, month-1, 1) automatically handles this by interpreting month 0 as December.
Can I use this for the first day of the current month?
Yes, simply use date(year, month, 1) or in Excel =EOMONTH(TODAY(),-1)+1. The same logic applies without the subtraction.
Why not just subtract 30 days?
Because months have different lengths. Subtracting 30 days from March 31 gives March 1, not February 1. Always subtract a month, not a fixed number of days, when you need the same calendar day in the previous month.
How do I do this in SQL?
In MySQL: SELECT DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, '%Y-%m-01'). In PostgreSQL: SELECT date_trunc('month', CURRENT_DATE - INTERVAL '1 month'). Both return the first day of the previous month.
Does this work for any date, not just today?
Yes, the formula works for any valid date. Replace the current date with any date you want, and it will return the first day of the month before that date.