- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements