When you work with dates in Python, you’ll often need the first day of a month. For example, to filter “this month’s” orders, to build a monthly report, or to calculate when the next billing cycle starts. This lesson walks you through each approach step by step.
This article is part of a series on Python Date and Time.
In this Python lesson, you’ll learn:
- How to get the first day of the month (start date) from any date.
- How to get the start of the month at midnight (00:00:00).
- How to get the first day of the next month and the previous month.
- How to find the first Monday (or any weekday) and the first business day of a month.
- How to do the same in pandas.
Also, see Python Get Last Day of Month.
Table of contents
How To Find The First Day Of The Month In Python
Let’s assume your application receives a date, and you want the first day of that month. For example, for 2022-09-14 you want 2022-09-01. Unlike the last day of the month, the first day is always day 1, so you only need to change the day. The steps below use the replace() method.
- Import the date or datetime class
The Python datetime module provides classes to create and work with dates and times. Use
from datetime import date(ordatetimeif you also need the time). - Get your input date
Store the date in a variable. Use
date.today()to get the current date if you want the first day of the current month. - Call replace(day=1)
The
replace()method returns a copy of the date with the fields you pass changed. Passingday=1changes only the day and keeps the year and month.
For example,date(2022, 8, 27).replace(day=1)returnsdate(2022, 8, 1), the first day of August 2022.
Example 1: Get the first day of the month using the replace() method
Output:
Date is: 2022-09-14 First day of the month: 2022-09-01 First day of this month: 2026-11-01
The last line uses today’s date, so your output will differ. replace() never changes the original object. Dates are immutable, so it always returns a new date.
Get the start of the month at midnight
If you call replace(day=1) on a datetime, the time stays the same. That’s a common source of bugs when you filter records “from the start of the month”. To get midnight, also reset the time fields:
Output:
Datetime is: 2022-09-15 15:43:55.304669 replace(day=1) only: 2022-09-01 15:43:55.304669 Start of the month: 2022-09-01 00:00:00
Another way is to combine the first date with midnight using datetime.combine(d.replace(day=1), time.min), where time is also imported from the datetime module.
Example 2: Get the first day of the month using the timedelta class
You can get the same result with the timedelta class, which represents a duration that you can add to or subtract from a date.
The idea: if today is the 14th, the first day of the month was 13 days ago. In general, subtract day - 1 days from the date.
- Import the
dateandtimedeltaclasses from the datetime module. - Get the day number of the input date from its
dayattribute. For example, for 2022-09-14 it is 14. - Subtract
timedelta(days=day - 1)from the input date.
Output:
Input date: 2022-09-14 First day of month: 2022-09-01
This works, but replace(day=1) is shorter and clearer, so prefer it in real code.
Get The First Day Of Next Month In Python
Now let’s get the first date of the next month. For example, for 2022-08-17, the first day of the next month is 2022-09-01. For 2021-12-31, it’s 2022-01-01, so the year must change too.
Using replace() and timedelta
Use the steps below:
- Move to the first day of the current month using
replace(day=1). - Add 32 days. From the 1st of any month, 32 days later is always in the next month, because no month has more than 31 days. It can’t overshoot either, because the next month has at least 28 days.
- Call
replace(day=1)again to move to the first day of that month.
Output:
2022-09-01 2022-01-01 2020-03-01
As you can see, it handles the end of the year and leap years correctly.
Using simple month arithmetic
You can also calculate the next month number directly. If the month is December, move to January of the next year:
Output:
2022-09-01 2022-01-01
Using dateutil
With the relativedelta class from the dateutil package (pip install python-dateutil), you can move by any number of months. Plural arguments such as months=1 add months, while singular arguments such as day=1 set the day.
Output:
Next month: 2022-09-01 In 3 months: 2022-11-01
Get The First Day Of Previous Month In Python
For example, for 2022-08-17, the first day of the previous month is 2022-07-01. For 2022-01-01, it is 2021-12-01.
Use the steps below:
- Move to the first day of the current month using
replace(day=1). - Subtract one day using
timedelta(days=1). This lands on the last day of the previous month. - Call
replace(day=1)again to move to the first day of that month.
Example: Get The First Day Of the Previous Month
Output:
2021-11-01 2021-12-01 2022-02-01
With dateutil, it’s one line: d + relativedelta(months=-1, day=1).
Get the First Monday (or Any Weekday) of the Month
Some events happen on “the first Monday of the month”. Start from the first day, then move forward by the number of days needed to reach the weekday you want. In Python, weekday() returns 0 for Monday through 6 for Sunday.
Output:
First Monday of Sep 2022: 2022-09-05 First Friday of Sep 2022: 2022-09-02
The modulo operator % 7 keeps the result between 0 and 6. If the month already starts on the weekday you want, days_ahead is 0, and the first day is returned.
Get the First Business Day of the Month
To get the first working day, start from day 1 and move forward while the date falls on Saturday (5) or Sunday (6).
Output:
2022-10-03 2022-09-01
For public holidays, see how to calculate business days in Python.
Get the First Day of the Month in pandas
For a pandas Series of dates, convert each date to a monthly period and take its start time. This also resets the time to midnight.
Output:
0 2022-09-01 1 2024-02-01 2 2022-12-01 dtype: datetime64[us] ['2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01']
Summary
| Task | Use |
|---|---|
| First day of the month | d.replace(day=1) |
| Start of the month at midnight | dt.replace(day=1, hour=0, minute=0, second=0, microsecond=0) |
| First day of the next month | (d.replace(day=1) + timedelta(days=32)).replace(day=1) |
| First day of the previous month | (d.replace(day=1) - timedelta(days=1)).replace(day=1) |
| Move N months with dateutil | d + relativedelta(months=N, day=1) |
| pandas | series.dt.to_period('M').dt.start_time |
