• 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 Days in Month Using Python

Get Days in Month Using Python

March 6, 2022 Leave a Comment

To get the days in 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.

import calendar
import datetime

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

print(currentDate)
print(daysInMonth)

#Output:
2022-03-06
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 want to get the number of days in a month.

To get the number of days in a month, we can use 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.

Below is a simple example in Python of how to get the number of days in a month of the current date using Python.

import calendar
import datetime

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

print(currentDate)
print(daysInMonth)

#Output:
2022-03-06
31

How to Get the Last Day of a Month Using Python

With the monthrange() function, we can get the last day of a month in Python easily by adjusting the code from above.

To get the last day of a given month with Python, we can get the number of days in a month using monthrange(), and then create a new date.

Below is how to use Python to create a new date that has the last day of a given 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 number of days in a month using Python.

Other Articles You'll Also Like:

  • 1.  Create List of Odd Numbers in Range with Python
  • 2.  Initialize Multiple Variables in Python
  • 3.  Get Python Dictionary Values as List
  • 4.  Convert String to Boolean Value in Python
  • 5.  nunique pandas – Get Number of Unique Values in DataFrame
  • 6.  Draw Rectangle in Python Using turtle Module
  • 7.  How to Remove Vowels from a String in Python
  • 8.  Using Python to Print Variable Type
  • 9.  pandas dropna – Drop Rows or Columns with NaN in DataFrame
  • 10.  Remove Leading and Trailing Characters from String with strip() 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

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