How to get last day of a month in Python?

PythonServer Side ProgrammingProgramming

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.

raja
Published on 08-Jan-2018 12:56:58
Advertisements