years()#

relationalai.std.dates
#years(n: int | Producer) -> Expression

Produces a period of n years. Must be called in a rule or query context.

Parameters#

NameTypeDescription
nint or ProducerThe number of years.

Returns#

An Expression that represents a period of n years.

Example#

Use years() to create a period of n years. You can add and subtract time periods from Date or DateTime values using the + and - operators:

#import relationalai as rai
from relationalai.std import alias
from relationalai.std.dates import date, years

# Create a model named "events" with an `Event` type.
model = rai.Model("events")
Event = model.Type("Event")

# Add an event with a start date.
with model.rule():
    Event.add(name="Conference", start_date=date(2021, 1, 1))

# Add 2 years to the event start date.
with model.query() as select:
    event = Event(name="Conference")
    new_date = event.start_date + years(2)
    response = select(event.name, alias(new_date, "new_date"))

print(response.results)
# Output:
#          name   new_date
# 0  Conference 2023-01-01

See date_add() and date_subtract() for more details on manipulating Date and DateTime values.

See Also#