How to get last day of a month in Python?


You can use the calendar module to find the weekday of first day of the month and number of days in month. Using this information you can easily get the Last day of the month. The calender module has a method, monthrange(year, month) that returns the weekday of first day of the month and number of days in month, for the specified year and month.

Example

import calendar
day, num_days = calendar.monthrange(2017, 12)
last_week = num_days % 7
last_day = (day + last_week) % 7
print(last_day)

Output

This will give the output −

0

Note that days are from 0-6 starting with sunday.

Updated on: 12-Jun-2020

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements