• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get First Day of Month Using Python

Get First Day of Month Using Python

March 6, 2022 3 Comments

To get the first day of the month using Python, the easiest way is to create a new date with datetime.date() and pass “1” as the third argument.

import datetime

currentDate = datetime.date.today()
firstDayOfMonth = datetime.date(currentDate.year, currentDate.month, 1)

print(currentDate)
print(firstDayOfMonth)

#Output:
2022-03-06
2022-03-01

When working in Python, many times we need to create variables which represent dates and times. When creating and displaying values related to dates, sometimes we need to display a particular day.

Getting the first day of a given month is very easy with Python.

To get the first day of a month, we just need to pass ‘1’ in the third argument of the date() function.

Below is how to create a date with the first day of the month in Python.

import datetime

currentDate = datetime.date.today()
firstDayOfMonth = datetime.date(currentDate.year, currentDate.month, 1)

print(currentDate)
print(firstDayOfMonth)

#Output:
2022-03-06
2022-03-01

How to Get the Last Day of the Month Using Python

If you instead want to get the last day of a month in Python, you can easily use the datetime module to do so.

We can easily get the last day of a month with the help of the monthrange function from the calendar module.

The monthrange() function takes in a year and month, and returns the weekday of first day of the month and number of days in month.

We can pass a year and month to monthrange() and then get the number of days of the month accessing the second element of the returned tuple.

After accessing the second element of the returned tuple, we create a new date using datetime.date().

Below is a simple example in Python of how to get last day of a month.

import calendar
import datetime

currentDate = datetime.date.today()
lastDayOfMonth = datetime.date(currentDate.year, currentDate.month, calendar.monthrange(currentDate.year, currentDate.month)[1])

print(currentDate)
print(lastDayOfMonth)

#Output:
2022-03-06
2022-03-31

Hopefully this article has been useful for you to learn how to get the first day of a month using Python.

Other Articles You'll Also Like:

  • 1.  Convert False to 0 in Python
  • 2.  Comparing Datetimes in Python
  • 3.  Examples of Recursion in Python
  • 4.  How to Use Python to Remove Zeros from List
  • 5.  Using Python to Add Trailing Zeros to String
  • 6.  Check if File Exists in AWS S3 Bucket Using Python
  • 7.  Ceiling Function Python – Get Ceiling of Number with math.ceil()
  • 8.  Pandas Crosstab on Multiple Columns
  • 9.  Python divmod() Function – Get Quotient and Remainder After Division
  • 10.  Not Equal Operator != in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Comments

  1. Jon says

    July 20, 2022 at 2:30 pm

    Thanks, this was helpful…you might consider changing your variable for the first day of the month to firstDayofMonth instead of lastDay as that threw me off a bit.

    Reply
    • Erik says

      July 20, 2022 at 3:15 pm

      Thanks for spell checking us – that was a silly mistake. It’s been fixed – thanks

      Reply
  2. Rahul says

    September 30, 2022 at 5:27 am

    I was looking extract first and last dates for every month. This helped me. Thank you so so much

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy