Last day of a month

EasyDatetime Programs · 9 of 20

Write a Python program to find the last day of a month.

Advertisement
Input
February 2028, November 2026
Output
2028-02-29
2026-11-30

Approach

  1. calendar.monthrange returns the number of days.

Solution

Python
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.

More datetime programs

← All 20 Datetime Programs

Leave a Comment