
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
- Related Articles
- How to get last day of the current month in MySQL?
- How to get last day of the previous month in MySQL?
- How to get last day of the next month in MySQL?
- Get the last day of month in Java
- Java Program to get day of week for the last day of each month in 2019
- How to get the last business day of previous month based on today in Excel?
- How to get the day of the month in JavaScript?
- How to display first day and last day of the month from date records in MySQL?
- How to get the first day of next month in MySQL?
- How to get first day of every corresponding month in MySQL?
- Python Pandas - Get day of the month that a Period falls on
- Python Pandas - Create a PeriodIndex and get the day of the month
- How to get a Date from year, month and day in Java?
- How to get the first day of the current month in MySQL?
- How to get the first day of the previous month in MySQL?

Advertisements