PYnative

Practice Coding

  • Python â–¼
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Coding Exercises â–¼
    • Python Exercises
    • C++ Exercises
    • C Programming Exercises
    • Java Exercises
    • C# Exercises
  • MCQsâ–¼
    • Python MCQ Quizzes
    • Java MCQ Quizzes
    • C++ MCQ Quizzes
  • Compilersâ–¼
    • Online Python Compiler
    • Online C Compiler
    • Online C++ Compiler
    • Online Java Compiler
    • Online C# Compiler
Home » Python » Python DateTime » Python Get First Day Of Month

Python Get First Day Of Month

Updated on: December 1, 2026 | 2 Comments

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
    • Example 1: Get the first day of the month using the replace() method
    • Get the start of the month at midnight
    • Example 2: Get the first day of the month using the timedelta class
  • Get The First Day Of Next Month In Python
    • Using replace() and timedelta
    • Using simple month arithmetic
    • Using dateutil
  • Get The First Day Of Previous Month In Python
    • Example: Get The First Day Of the Previous Month
  • Get the First Monday (or Any Weekday) of the Month
  • Get the First Business Day of the Month
  • Get the First Day of the Month in pandas
  • Summary

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.

  1. 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 (or datetime if you also need the time).

  2. 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.

  3. Call replace(day=1)

    The replace() method returns a copy of the date with the fields you pass changed. Passing day=1 changes only the day and keeps the year and month.

    For example, date(2022, 8, 27).replace(day=1) returns date(2022, 8, 1), the first day of August 2022.

Example 1: Get the first day of the month using the replace() method

from datetime import date

input_dt = date(2022, 9, 14)
print('Date is:', input_dt)

res = input_dt.replace(day=1)
print('First day of the month:', res)

# works for the current month too
print('First day of this month:', date.today().replace(day=1))Code language: Python (python)

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:

from datetime import datetime

now = datetime(2022, 9, 15, 15, 43, 55, 304669)
print('Datetime is:', now)

print('replace(day=1) only:', now.replace(day=1))

start_of_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
print('Start of the month:', start_of_month)Code language: Python (python)

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.

  1. Import the date and timedelta classes from the datetime module.
  2. Get the day number of the input date from its day attribute. For example, for 2022-09-14 it is 14.
  3. Subtract timedelta(days=day - 1) from the input date.
from datetime import date, timedelta

input_dt = date(2022, 9, 14)
print('Input date:', input_dt)

res = input_dt - timedelta(days=input_dt.day - 1)
print('First day of month:', res)Code language: Python (python)

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:

  1. Move to the first day of the current month using replace(day=1).
  2. 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.
  3. Call replace(day=1) again to move to the first day of that month.
from datetime import date, timedelta

def first_day_of_next_month(d):
    return (d.replace(day=1) + timedelta(days=32)).replace(day=1)

print(first_day_of_next_month(date(2022, 8, 26)))
print(first_day_of_next_month(date(2021, 12, 31)))
print(first_day_of_next_month(date(2020, 2, 29)))Code language: Python (python)

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:

from datetime import date

def first_day_of_next_month(d):
    if d.month == 12:
        return date(d.year + 1, 1, 1)
    return date(d.year, d.month + 1, 1)

print(first_day_of_next_month(date(2022, 8, 26)))
print(first_day_of_next_month(date(2021, 12, 31)))Code language: Python (python)

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.

from datetime import date
from dateutil.relativedelta import relativedelta

d = date(2022, 8, 26)
print('Next month:', d + relativedelta(months=1, day=1))
print('In 3 months:', d + relativedelta(months=3, day=1))Code language: Python (python)

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:

  1. Move to the first day of the current month using replace(day=1).
  2. Subtract one day using timedelta(days=1). This lands on the last day of the previous month.
  3. Call replace(day=1) again to move to the first day of that month.

Example: Get The First Day Of the Previous Month

from datetime import date, timedelta

def first_day_of_previous_month(d):
    last_day_prev = d.replace(day=1) - timedelta(days=1)
    return last_day_prev.replace(day=1)

print(first_day_of_previous_month(date(2021, 12, 31)))
print(first_day_of_previous_month(date(2022, 1, 1)))
print(first_day_of_previous_month(date(2022, 3, 15)))Code language: Python (python)

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.

from datetime import date, timedelta

def first_weekday_of_month(year, month, weekday):
    first = date(year, month, 1)
    days_ahead = (weekday - first.weekday()) % 7
    return first + timedelta(days=days_ahead)

MONDAY, FRIDAY = 0, 4
print('First Monday of Sep 2022:', first_weekday_of_month(2022, 9, MONDAY))
print('First Friday of Sep 2022:', first_weekday_of_month(2022, 9, FRIDAY))Code language: Python (python)

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).

from datetime import date, timedelta

def first_business_day(year, month):
    d = date(year, month, 1)
    while d.weekday() >= 5:
        d += timedelta(days=1)
    return d

print(first_business_day(2022, 10))   # 1 Oct 2022 is a Saturday
print(first_business_day(2022, 9))Code language: Python (python)

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.

import pandas as pd

dates = pd.to_datetime(pd.Series(['2022-09-14 15:30', '2024-02-29 08:00', '2022-12-01 00:00']))

print(dates.dt.to_period('M').dt.start_time)

# first day of each month in a year
print(pd.date_range('2022-01-01', periods=4, freq='MS').strftime('%Y-%m-%d').tolist())Code language: Python (python)

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

TaskUse
First day of the monthd.replace(day=1)
Start of the month at midnightdt.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 dateutild + relativedelta(months=N, day=1)
pandasseries.dt.to_period('M').dt.start_time

Filed Under: Python, Python DateTime

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python DateTime

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises
Java Exercises
C# Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes
In: Python Python DateTime
TweetF  sharein  shareP  Pin

 Python DateTime

  • Python DateTime Guide
  • Python Get Current DateTime
  • Python DateTime Formatting
  • Python String to DateTime
  • Python Timestamp
  • Python Timedelta
  • Python TimeZones
  • List All TimeZones in Python

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

Python Basics Python Exercises Python Quizzes Python Interview Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is a platform built to help developers learn by doing. Solve coding exercises, take MCQs, and run code directly in built-in online compilers.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises
  • Java Exercises
  • C# Exercises

MCQ Quizzes

  • Python MCQs
  • Java MCQs
  • C++ MCQs

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com