Date

final class whenever.Date(iso_string: str, /)[source]
final class whenever.Date(py_date: date, /)
final class whenever.Date(year: int, month: int, day: int)

A date without a time component.

>>> d = Date(2021, 1, 2)
Date("2021-01-02")

Can also be constructed from an ISO 8601 string or a standard library date:

>>> Date("2021-01-02")
Date("2021-01-02")
>>> Date(date(2021, 1, 2))
Date("2021-01-02")

Dates support arithmetic with ItemizedDateDelta:

>>> delta = Date("2021-02-28").since(Date("1994-05-15"), in_units=["years", "days"])
ItemizedDateDelta("P26y289d")
>>> Date("1994-05-15").add(delta)
Date("2021-02-28")

Dates can be compared and sorted:

>>> Date(2021, 1, 2) > Date(2021, 1, 1)
True
classmethod from_py_date(d: date, /) Date[source]

Create from a date

>>> Date.from_py_date(date(2021, 1, 2))
Date("2021-01-02")

Deprecated since version 0.10.0: Use the constructor Date(d) instead.

classmethod parse(s: str, /, *, format: str) Date[source]

Parse a date from a custom pattern string.

See Pattern format for details.

>>> Date.parse("2024/03/15", format="YYYY/MM/DD")
Date("2024-03-15")
>>> Date.parse("15 Mar 2024", format="DD MMM YYYY")
Date("2024-03-15")
classmethod parse_iso(s: str, /) Date[source]

Parse a date from an ISO8601 string

The following formats are accepted: - YYYY-MM-DD (“extended” format) - YYYYMMDD (“basic” format)

Inverse of format_iso()

>>> Date.parse_iso("2021-01-02")
Date("2021-01-02")
classmethod today_in_system_tz() Date[source]

Get the current date in the system’s local timezone.

Alias for Instant.now().to_system_tz().date().

>>> Date.today_in_system_tz()
Date("2021-01-02")
__add__(p: DateDelta) Date[source]

Add a delta to a date. Behaves the same as add()

Deprecated since version 0.10.0: Using the + operator on Date is deprecated; use the add() method instead.

__eq__(other: object) bool[source]

Compare for equality

>>> d = Date(2021, 1, 2)
>>> d == Date(2021, 1, 2)
True
>>> d == Date(2021, 1, 3)
False
__format__(spec: str, /) str[source]

Default object formatter.

Return str(self) if format_spec is empty. Raise TypeError otherwise.

__ge__(other: Date) bool[source]

Return self>=value.

__gt__(other: Date) bool[source]

Return self>value.

__le__(other: Date) bool[source]

Return self<=value.

__lt__(other: Date) bool[source]

Return self<value.

__str__(*, basic: bool = False) str

Format as the ISO 8601 date format.

Inverse of parse_iso().

>>> Date(2021, 1, 2).format_iso()
'2021-01-02'
>>> Date(1992, 9, 4).format_iso(basic=True)
'19920904'
__sub__(d: DateDelta) Date[source]
__sub__(d: Date) DateDelta

Subtract a delta from a date, or subtract two dates

Subtracting a delta works the same as subtract().

>>> Date(2021, 1, 2) - DateDelta(weeks=1, days=3)
Date("2020-12-26")

The difference between two dates is calculated in months and days, such that:

>>> delta = d1 - d2
>>> d2 + delta == d1  # always

The following is not always true:

>>> d1 - (d1 - d2) == d2  # not always true!
>>> -(d2 - d1) == d1 - d2  # not always true!
>>> Date(2023, 4, 15) - Date(2011, 6, 24)
DateDelta("P12Y9M22D")
>>> # Truncation
>>> Date(2024, 4, 30) - Date(2023, 5, 31)
DateDelta("P11M")
>>> Date(2024, 3, 31) - Date(2023, 6, 30)
DateDelta("P9M1D")
>>> # the other way around, the result is different
>>> Date(2023, 6, 30) - Date(2024, 3, 31)
DateDelta(-P9M)

Deprecated since version 0.10.0: Using the - operator on Date is deprecated; use the subtract() method or the since() method instead.

add(delta: ItemizedDateDelta | DateDelta, /) Date[source]
add(*, years: int = ..., months: int = ..., weeks: int = ..., days: int = ...) Date

Add a components to a date.

See the docs on arithmetic for more information.

>>> d = Date(2021, 1, 2)
>>> d.add(years=1, months=2, days=3)
Date("2022-03-05")
>>> Date(2020, 2, 29).add(years=1)
Date("2021-02-28")
at(t: Time, /) PlainDateTime[source]

Combine a date with a time to create a datetime

>>> d = Date(2021, 1, 2)
>>> d.at(Time(12, 30))
PlainDateTime("2021-01-02 12:30:00")

You can use methods like assume_utc() or assume_tz() to find the corresponding exact time.

>>> d.at(Time(12, 30)).assume_tz("America/New_York")
ZonedDateTime("2021-01-02 12:30:00-05:00[America/New_York]")
day_of_week() Weekday[source]

The day of the week

>>> Date(2021, 1, 2).day_of_week()
Weekday.SATURDAY
>>> Weekday.SATURDAY.value
6  # the ISO value
days_since(other: Date, /) int[source]

Calculate the number of days this day is after another date.

Deprecated since version 0.10.0: Use since() with unit=”days” instead.

days_until(other: Date, /) int[source]

Calculate the number of days from this date to another date.

Deprecated since version 0.10.0: Use until() with unit=”days” instead.

format(pattern: str, /) str[source]

Format as a custom pattern string.

See Pattern format for details.

>>> Date(2024, 3, 15).format("YYYY/MM/DD")
'2024/03/15'
>>> Date(2024, 3, 15).format("DD MMM YYYY")
'15 Mar 2024'
format_iso(*, basic: bool = False) str[source]

Format as the ISO 8601 date format.

Inverse of parse_iso().

>>> Date(2021, 1, 2).format_iso()
'2021-01-02'
>>> Date(1992, 9, 4).format_iso(basic=True)
'19920904'
month_day() MonthDay[source]

The month and day (without a year component)

>>> Date(2021, 1, 2).month_day()
MonthDay("--01-02")
py_date() date[source]

Convert to a standard library date

Deprecated since version 0.10.0: Use to_stdlib() instead.

replace(year: int = ..., month: int = ..., day: int = ...) Date[source]

Create a new instance with the given fields replaced

>>> d = Date(2021, 1, 2)
>>> d.replace(day=4)
Date("2021-01-04")
since(b: Date, /, *, total: DateDeltaUnitStr) float[source]
since(
b: Date,
/,
*,
in_units: Sequence[TypeAliasForwardRef('DateDeltaUnitStr')],
round_mode: RoundModeStr = 'trunc',
round_increment: int = ...,
) ItemizedDateDelta

Calculate the difference between this date and another date. The difference is calculated in terms of the chosen calendar unit or units.

>>> d = Date(2023, 4, 15)
>>> d.since(Date("2020-01-01"), in_units=["years", "months"])
ItemizedDateDelta("P3y3m")
>>> d.since(Date("2020-01-01"), total="weeks")
170.0
Parameters:
  • other – The date to calculate the difference since.

  • total

    If specified, the difference is returned as a float in terms of this single unit. Cannot be combined with in_units.

    The fractional part is based on the number of days in the surrounding calendar period — not a fixed conversion factor. For example, 6 months from January 1 spans 181 days of a 365-day year, giving approximately 0.496 years, not 0.5.

  • in_units – If specified, the difference is calculated in terms of these units, in decreasing order of size. Cannot be combined with total.

  • round_mode – The rounding mode to apply to the smallest specified unit. Only valid with in_units.

  • round_increment – The increment to round to for the smallest specified unit. Only valid with in_units.

Returns:

If in_units is specified, the difference is returned as an ItemizedDateDelta, If total is specified, as a float number of the specified unit.

Return type:

ItemizedDateDelta | float

subtract(delta: ItemizedDateDelta | DateDelta, /) Date[source]
subtract(*, years: int = ..., months: int = ..., weeks: int = ..., days: int = ...) Date

Subtract components from a date.

See the docs on arithmetic for more information.

>>> d = Date(2021, 1, 2)
>>> d.subtract(years=1, months=2, days=3)
Date("2019-10-30")
>>> Date(2021, 3, 1).subtract(years=1)
Date("2020-03-01")
to_stdlib() date[source]

Convert to a standard library date

until(b: Date, /, *, total: DateDeltaUnitStr) float[source]
until(
b: Date,
/,
*,
in_units: Sequence[TypeAliasForwardRef('DateDeltaUnitStr')],
round_mode: RoundModeStr = 'trunc',
round_increment: int = ...,
) ItemizedDateDelta

Companion to since() that calculates the difference until another date. See since() for more information.

year_month() YearMonth[source]

The year and month (without a day component)

>>> Date(2021, 1, 2).year_month()
YearMonth("2021-01")
MAX: ClassVar[Date] = Date("9999-12-31")

The maximum possible date

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

The minimum possible date

property day: int

The day component of the date

>>> Date(2021, 1, 2).day
2
property month: int

The month component of the date

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

The year component of the date

>>> Date(2021, 1, 2).year
2021