date.fromdatetime()#
relationalai.std.dates
#date.fromdatetime(datetime: datetime | Producer) -> Expression
Constructs a Date
value from a DateTime
value.
Must be called in a rule or query context.
The time components of the DateTime
are discarded.
Parameters#
Name | Type | Description |
---|---|---|
datetime | datetime or Producer | The DateTime value to convert to Date . |
Returns#
An Expression
that produces Date
values.
Example#
Use date.fromdatetime()
to construct a Date
value from a DateTime
value:
#import relationalai as rai
from relationalai.std.dates import date, datetime
# Create a model named "events" with an `Event` type.
model = rai.Model("events")
Event = model.Type("Event")
# Add an event with a start datetime.
with model.rule():
# The event starts on January 1, 2021, at 09:00:00 UTC.
Event.add(
name="Conference",
starts_at=datetime(2021, 1, 1, 9, 0, 0, tz="UTC")
)
# Convert the start datetime to a start date.
with model.rule():
event = Event()
event.set(
start_date=date.fromdatetime(event.starts_at)
)
with model.query() as select:
event = Event()
response = select(event.name, event.start_date)
print(response.results)
# Output:
# name start_date
# 0 Conference 2021-01-01
You can also use object property values as datetimes:
#import relationalai as rai
from relationalai.std.dates import date, datetime
# Create a model named "events" with an `Event` type.
model = rai.Model("events")
Event = model.Type("Event")
# Add events with start datetimes.
with model.rule():
Event.add(
name="Conference",
starts_at=datetime(2021, 1, 1, 9, 0, 0, tz="UTC")
)
Event.add(
name="Workshop",
starts_at=datetime(2021, 1, 2, 10, 0, 0, tz="UTC")
)
# Create a `start_date` property using the `date.fromdatetime()` constructor.
with model.rule():
event = Event()
event.set(
start_date=date.fromdatetime(event.starts_at)
)
with model.query() as select:
event = Event()
response = select(event.name, event.start_date)
print(response.results)
# Output:
# name start_date
# 0 Conference 2021-01-01
# 1 Workshop 2021-01-02