• 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
  • VBA
  • About
You are here: Home / Python / Get Last Day of Month Using Python

Get Last Day of Month Using Python

March 6, 2022 Leave a Comment

To get the last day of the month using Python, the easiest way is with the timerange() function from the calendar module to get the number of days in the month, and then create a new date.

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

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.

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

How to Get the First Day of the Month Using Python

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

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()
lastDayOfMonth = datetime.date(currentDate.year, currentDate.month, 1)

print(currentDate)
print(lastDayOfMonth)

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

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

Other Articles You'll Also Like:

  • 1.  Python Get First Word in String
  • 2.  Using Python Selenium Webdriver to Refresh Page
  • 3.  Python cosh – Find Hyperbolic Cosine of Number Using math.cosh()
  • 4.  Check if Variable is Integer in Python
  • 5.  Find Maximum of Three Numbers in Python
  • 6.  Using Python to Sum the Digits of a Number
  • 7.  Python cube root – Find Cube Root of Number With math.pow() Function
  • 8.  Python Square Root Without Math Module – ** or Newton’s Method
  • 9.  Using Python to Count Even Numbers in List
  • 10.  pandas mad – Calculate Mean Absolute Deviation 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

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy