dayofyear()#
relationalai.std.dates
#dayofyear(date: date | datetime | Producer, tz: str = "UTC") -> Expression
Extracts the day of the year from a Date
or DateTime
value as an integer.
If date
is a Python date
or datetime
object, the tz
parameter is ignored.
Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
date | date or datetime or Producer | The Date or DateTime value from which to extract the day of the year. |
tz | str | Optional timezone component for DateTime values (default is “UTC”). Ignored if date is a Python date or datetime object. |
Returns#
An Expression
that produces the day of the year of the given Date
or DateTime
value, as an integer in the range 1 to 365 (or 366 in a leap year).
Raises#
TypeError
if the date
parameter is not a date
, datetime
, or Producer
object.
Example#
Use dayofyear()
to extract the day of the year from a Date
value:
#import relationalai as rai
from relationalai.std.dates import dayofyear, date
from relationalai.std import alias
# Create a model named "people" with a `Person` type.
model = rai.Model("people")
Person = model.Type("Person")
# Add a person with a birth date.
with model.rule():
Person.add(name="Alice", birth_date=date(1990, 5, 23))
# Extract the day of the year from birth_date.
with model.query() as select:
person = Person()
birth_dayofyear = dayofyear(person.birth_date)
response = select(person.name, alias(birth_dayofyear, "birth_dayofyear"))
print(response.results)
# Output:
# name birth_dayofyear
# 0 Alice 143
You can also use dayofyear()
to extract the day of the year from a DateTime
value:
#import relationalai as rai
from relationalai.std.dates import dayofyear, datetime
from relationalai.std import alias
# Create a model named "people" with a `Person` type.
model = rai.Model("people")
Person = model.Type("Person")
# Add a person with a birth datetime.
with model.rule():
Person.add(
name="Bob",
born_at=datetime(1985, 8, 15, 15, 45, 0, tz="UTC")
)
# Extract the day of the year from born_at.
with model.query() as select:
person = Person()
birth_dayofyear = dayofyear(person.born_at)
response = select(person.name, alias(birth_dayofyear, "birth_dayofyear"))
print(response.results)
# Output:
# name birth_dayofyear
# 0 Bob 227