Partial types

This section describes the “smaller” date & time types provided by whenever: Date, Time, YearMonth, and MonthDay.

Overview

Type

Represents

Example

Date

A calendar date (year, month, day)

Date(2024, 3, 15)

Time

A time of day (hour, minute, second…)

Time(14, 30)

YearMonth

A year and month without a day

YearMonth(2024, 3)

MonthDay

A month and day without a year

MonthDay(3, 15)

Date

Date represents a calendar date. It supports arithmetic with ItemizedDateDelta and calculating the difference between two dates:

>>> d = Date(2023, 1, 31)
>>> d.add(months=1)         # End-of-month pinning
Date("2023-02-28")
>>> d.since(Date(2022, 10, 15), in_units=["months", "days"])
ItemizedDateDelta("P3m16d")

You can combine a Date with a Time to get a PlainDateTime:

>>> Date(2023, 6, 15).at(Time(9, 0))
PlainDateTime("2023-06-15T09:00:00")

Time

Time represents a time of day, independent of any date or timezone. Sub-second precision is supported down to nanoseconds.

>>> Time(14, 30, nanosecond=500_000_000)
Time("14:30:00.5")

YearMonth and MonthDay

YearMonth and MonthDay are useful for recurring events or partial date specifications (e.g. a birthday or an annual deadline):

>>> YearMonth(2024, 3).on_day(22)
Date("2024-03-22")
>>> MonthDay(2, 29).in_year(2024)
Date("2024-02-29")