Python Glossary Русский

Python 3.12+calendarterm

calendar.Month

days and months

An enumeration of months (IntEnum) from JANUARY = 1 to DECEMBER = 12; its members are also exposed as calendar.JANUARY…calendar.DECEMBER.

Syntax

calendar.Month.JANUARY  # … calendar.Month.DECEMBER

Months are numbered from one, while weekdays in Day start at zero: the habit of counting from zero gives an off-by-one error here. That is why month_name[0] is an empty string and month_name has 13 items. The old mixed-case names January and February were deprecated in 3.12 — use JANUARY or Month.JANUARY. Being an IntEnum, a member can be passed to monthrange() and used as an index.

Examples

import calendar
print(calendar.Month.JANUARY.value, calendar.Day.MONDAY.value)  # → 1 0
print(calendar.Month(12).name)  # → DECEMBER
print(len(calendar.month_name), repr(calendar.month_name[0]))  # → 13 ''
import calendar
first, days = calendar.monthrange(2024, calendar.Month.FEBRUARY)
print(days)  # → 29

See also

Open in the full glossarydocs.python.orgReport an inaccuracy