datetime.fromdate()#
relationalai.std.dates
#datetime.fromdate(date: date | Producer) -> Expression
Constructs a DateTime
value from a Date
value.
Must be called in a rule or query context.
The sub-day components of the resulting DateTime
are set to 0.
To construct a Date
value, use the date.fromdatetime
function.
Parameters#
Name | Type | Description |
---|---|---|
date | date or Producer | The Date value to convert to DateTime . |
Returns#
An Expression
that produces DateTime
values.
Example#
Use datetime.fromdate()
to construct a DateTime
value from a Date
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 date.
with model.rule():
# The event starts on January 1, 2021.
Event.add(
name="Conference",
start_date=date(2021, 1, 1)
)
# Convert the start date to a start datetime.
with model.rule():
event = Event()
event.set(
starts_at=datetime.fromdate(event.start_date)
)
with model.query() as select:
event = Event()
response = select(event.name, event.starts_at)
print(response.results)
# Output:
# name starts_at
# 0 Conference 2021-01-01
You can also use object property values as dates:
#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 dates.
with model.rule():
Event.add(
name="Conference",
start_date=date(2021, 1, 1)
)
Event.add(
name="Workshop",
start_date=date(2021, 1, 2)
)
# Create a start_datetime property using the datetime.fromdate() constructor.
with model.rule():
event = Event()
event.set(
start_datetime=datetime.fromdate(event.start_date)
)
with model.query() as select:
event = Event()
response = select(event.name, event.start_datetime)
print(response.results)
# Output:
# name start_datetime
# 0 Conference 2021-01-01
# 1 Workshop 2021-01-02