When you work on a datetime in Python, you’ll likely need a way to find the first day of the month. Here’s a teachable Python lesson that walks through the steps to complete that coding task.
In this Python lesson, you’ll learn:
- How to get the first day of the month (start date)
- How to get the first day of next month and the previous month
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 datetime object, and you want to convert the given date to the first day of the month. For example, the datetime is ‘2022-9-14‘ and you want to write a code that will return you ‘2022-9-1‘, i.e., the first day of the month or the start date of a month.
The below steps show how to use the datetime.replace()
method to find the first day of a month of a datetime object.
- Import datetime class from a datetime module
Python datetime module provides various functions to create and manipulate the date and time. Use the
from datetime import datetime
statement to import adatetime
class from a datetime module. - Store datetime object in a variable
Store the input datetime object in a variable for further processing. Or you can use the
datetime.today()
to get the current datetime if you want to find the start date of a current month. - Use the datetime.replace() method
The
datetime.replace()
function replaces the DateTime object’s contents with the given parameters. This method has various parameters, but here we will use theday
parameter because we want to replace the day number of a datetime object with the 1 (first day of the month).
For example,datetime(2022, 8, 27).replace(day=1)
will returndatetime(2022, 8, 1) i.e., nothing but the
first day of a month or a start date of a month.
Example 1: Get the first day of the month using the replace() method
from datetime import datetime
# get today's datetime
input_dt = datetime.today()
print('Datetime is:', input_dt)
res = input_dt.replace(day=1)
print('First day of a month:', res)
# print only date
print('Only date:', res.date())
Output:
Datetime is: 2022-09-15 15:43:55.304669 First day of a month: 2022-09-01 15:43:55.304669 Only date: 2022-09-01
Example 2: Get the first day of the month using the timedelta class
We can get the same result using the timedelta class of a datetime module.
A timedelta represents a duration which is the difference between two dates, time, or datetime instances, to the microsecond resolution. We can use the timedelta to add or subtract weeks, days, hours, minutes, seconds, microseconds, and milliseconds from a given DateTime.
Use the day
parameter of a timedelta class to replace the day number of a datetime object with the 1 (first day of the month) to find the first day of the month using a specific date.
Use the below steps:
- First, import the datetime and timedelta class from a datetime module.
- Next, get the actual day number of an input datetime object using the strftime() method. For example, if datetime is ‘2022-8-24′, it will return ’24’.
- Next, use the timedelta class to subtract the actual day – 1 from an input date to get the first day of the month (starting date of a month)
Example:
from datetime import datetime, timedelta
input_dt = datetime.today().date()
print('Input date:', input_dt)
# get the actual day number
day_num = input_dt.strftime("%d")
# subtract those number of days from input date
# using the timedelta
res = input_dt - timedelta(days=int(day_num) - 1)
print('First day of month:', res)
Output:
Input date: 2022-09-15 First day of month: 2022-09-01
Also, see Python Get Last Day of Month.
Get The First Day Of Next Month In Python
Let’s see how to get the first date of the next month in Python. For example, if datetime contains 2022-08-17, the first day of the next month is 2022-09-01. Also, if the datetime is ‘2021-12-31’, the first day of the next month is 2022-01-01.
Using the timedelta class from a DateTime module, we can get the first date of the next month.
Use the below steps.
- Import datetime and timedelta class from datetime module
- Replace the day number of a datetime to 1 using the
datetime.replace()
method - Next, pick a number that certainly is more than any month but less than any two months combined and add them to input datetime using timedelta
- Next, again replace the day number of an input datetime to 1 using the
datetime.replace()
method
Example: Get The First Day Of Next Month
from datetime import datetime, timedelta
input_dt = datetime(2022, 8, 26)
print('Input date:', input_dt.date())
# first replace day number with 1
input_dt = input_dt.replace(day=1)
# add 32 days to the input datetime
input_dt = input_dt + timedelta(days=32)
# replace day number with 1
res = input_dt.replace(day=1)
print('First day of month:', res.date())
Output:
Input date: 2022-08-26 First day of month: 2022-09-01
The above solution works for all cases. See the below example.
Example:
from datetime import datetime, timedelta
input_dt = datetime(2021, 12, 31)
print((input_dt.replace(day=1) + timedelta(days=32)).replace(day=1))
input_dt = datetime(2020, 2, 29)
print((input_dt.replace(day=1) + timedelta(days=32)).replace(day=1))
input_dt = datetime(2022, 12, 1)
print((input_dt.replace(day=1) + timedelta(days=32)).replace(day=1))
Output:
2022-01-01 00:00:00 2020-03-01 00:00:00 2023-01-01 00:00:00
Get The First Day Of Previous Month In Python
Let’s see how to get the first date of the previous month in Python. For example, if datetime contains 2022-08-17, the first day of the previous month is 2022-07-01. Also, if the datetime is ‘2022-1-1’, the first day of the previous month is 2021-12-01.
Using the timedelta class from a DateTime module, we can get the first day of the previous month.
Use the below steps.
- Import datetime and timedelta class from datetime module
- Replace the day number of an input datetime to 1 using the
datetime.replace(day=1)
method - Use timedelta to backup a single day to the last day of the previous month.
- Next, pick a number that certainly is more than any month but less than any two months combined and add them to input datetime using timedelta
- Next, again replace the day number of an input datetime to 1 using the
datetime.replace(day=1)
method
Example: Get The First Day Of the Previous Month
from datetime import datetime, timedelta
input_dt = datetime(2021, 12, 31)
print('Input datetime:', input_dt.date())
# first replace day number with 1
dt = input_dt.replace(day=1)
# subtract 1 day from input datetime
# go back to previous month i.e. last date of previous month
dt = dt - timedelta(days=1)
# replace day number with 1
res = dt.replace(day=1)
print('first day of a previous month:', res.date())
Output:
Input datetime: 2021-12-31 first day of a previous month: 2021-11-01