Python Practice › Datetime Programs › Program 9
Last day of a month
Write a Python program to find the last day of a month.
Input
February 2028, November 2026
Output
2028-02-29 2026-11-30
Approach
calendar.monthrangereturns the number of days.
Solution
import calendarfrom datetime import date for year, month in [(2028, 2), (2026, 11)]: days = calendar.monthrange(year, month)[1] print(date(year, month, days))Output
2028-02-29 2026-11-30
Learn the concept
New to this topic? Read the lesson, then come back.