relationalai.std.dates.date.fromdatetime()
date.fromdatetime(datetime: datetime|Producer, tz: tzinfo|str|Producer = "UTC") -> ExpressionConstructs a date value from a datetime value by discarding the time components.
If tz is specified, the datetime is converted to the specified timezone or offset string before extracting the date.
Must be called in a rule or query context.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
datetime | Producer or Python datetime object. | The datetime value to convert to a date. |
tz | tzinfo or str or Producer | Optional timezone string (e.g., "America/New_York"), offset string (e.g., "+0600"), or Python tzinfo object. Refer to the timezone database for a list of valid timezone indentifiers. (Default: "UTC"). |
Returns
Section titled “Returns”An Expression object.
Example
Section titled “Example”Use date.fromdatetime() to construct a date value from a datetime value:
import relationalai as raifrom relationalai.std import dates
# =====# SETUP# =====
model = rai.Model("MyModel")Event = model.Type("Event")
# Add an event with a time property.with model.rule(): Event.add(id=1).set(time=dates.datetime(2020, 1, 1, 1)) # 1:00 AM on January 1, 2020
# =======# EXAMPLE# =======
# Set a date property using the time property.with model.rule(): event = Event() event.set(date=dates.date.fromdatetime(event.time))
with model.query() as select: event = Event() response = select(event.id, event.time, event.date)
print(response.results)# id time date# 0 1 2020-01-01 01:00:00 2020-01-01Pass a timezone or offset string to the tz parameter to convert the datetime from UTC to a different timezone before extracting the date:
with model.query() as select: event = Event() date1 = dates.date.fromdatetime(event.time, tz="America/New_York") date2 = dates.date.fromdatetime(event.time, tz="+0600") response = select(event.id, event.time, alias(date1, "date1"), alias(date2, "date2"))
print(response.results)# id time date1 date2# 0 1 2020-01-01 01:00:00 2019-12-31 2020-01-01Refer to the timezone database for a list of valid timezone identifiers.