weekday()#

relationalai.std.dates
#weekday(date: date | datetime | Producer, tz: str = "UTC") -> Expression

Extracts the day of the week from a Date or DateTime value as an integer where Monday is 0 and Sunday is 6. If date is a Python date or datetime object, the tz parameter is ignored. Must be called in a rule or query context.

Parameters#

NameTypeDescription
datedate or datetime or ProducerThe Date or DateTime value from which to extract the day of the week.
tzstrOptional 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 week of the given Date or DateTime value, as an integer in the range 0 to 6, where 0 represents Monday and 6 represents Sunday.

Raises#

TypeError if the date parameter is not a date, datetime, or Producer object.

Example#

Use weekday() to extract the day of the week from a Date value:

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

# 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 weekday from birth_date.
with model.query() as select:
    person = Person()
    birth_weekday = weekday(person.birth_date)
    response = select(person.name, alias(birth_weekday, "birth_weekday"))

print(response.results)
# Output:
#     name  birth_weekday
# 0  Alice              2

You can also use weekday() to extract the day of the week from a DateTime value:

#import relationalai as rai
from relationalai.std import alias
from relationalai.std.dates import weekday, datetime

# 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 weekday from `born_at`.
with model.query() as select:
    person = Person()
    birth_weekday = weekday(person.born_at)
    response = select(person.name, alias(birth_weekday, "birth_weekday"))

print(response.results)
# Output:
#   name  birth_weekday
# 0  Bob              3

See Also#