Dev Helpers

Python / Dates and calendars

Get month length

Dev Helpers · September 27, 2025

Month length depends on both the month and the year. Ask the calendar rather than maintaining a table with a special February branch.

Worked example

import calendar
print(calendar.monthrange(2026, 2)[1])

Result

28

Implementation notes

Calendar rules depend on which calendar is being modeled. Use a date or calendar primitive throughout, and defer localized month names to display.

Try it yourself

Check February 2000 against February 2100.

Open the Python example →

Editorial date September 27, 2025 · Published December 6, 2026

Related articles

Run an async function

An async function call creates a coroutine. A top-level runner supplies the event loop that executes it.

Transpose a matrix

Transposing rows into columns assumes a decision about uneven rows. Ordinary zip stops at the shortest input.

Unpack with a remainder

Starred unpacking separates a fixed header from a variable-length tail. It still requires enough elements for the fixed positions.