YearMonth

final class whenever.YearMonth(iso_string: str, /)[source]
final class whenever.YearMonth(year: int, month: int)

A year and month without a day component.

Useful for representing recurring events, billing periods, or any concept that doesn’t need a specific day.

>>> ym = YearMonth(2021, 1)
YearMonth("2021-01")

Can also be constructed from an ISO 8601 string:

>>> YearMonth("2021-01")
YearMonth("2021-01")
classmethod parse_iso(s: str, /) YearMonth[source]

Create from the ISO 8601 format YYYY-MM or YYYYMM.

Inverse of format_iso()

>>> YearMonth.parse_iso("2021-01")
YearMonth("2021-01")
__eq__(other: object) bool[source]

Compare for equality

>>> ym = YearMonth(2021, 1)
>>> ym == YearMonth(2021, 1)
True
>>> ym == YearMonth(2021, 2)
False
__ge__(other: YearMonth) bool[source]

Return self>=value.

__gt__(other: YearMonth) bool[source]

Return self>value.

__le__(other: YearMonth) bool[source]

Return self<=value.

__lt__(other: YearMonth) bool[source]

Return self<value.

__str__() str

Format as the ISO 8601 year-month format.

Inverse of parse_iso().

>>> YearMonth(2021, 1).format_iso()
'2021-01'
format_iso() str[source]

Format as the ISO 8601 year-month format.

Inverse of parse_iso().

>>> YearMonth(2021, 1).format_iso()
'2021-01'
on_day(day: int, /) Date[source]

Create a date from this year-month with a given day

>>> YearMonth(2021, 1).on_day(2)
Date("2021-01-02")
replace(year: int = ..., month: int = ...) YearMonth[source]

Create a new instance with the given fields replaced

>>> d = YearMonth(2021, 12)
>>> d.replace(month=3)
YearMonth("2021-03")
MAX: ClassVar[YearMonth] = YearMonth("9999-12")

The maximum possible year-month

MIN: ClassVar[YearMonth] = YearMonth("0001-01")

The minimum possible year-month

property month: int

The month component of the year-month

>>> YearMonth(2021, 1).month
1
property year: int

The year component of the year-month

>>> YearMonth(2021, 1).year
2021