DateTime()#

relationalai.std.dates
#DateTime(val: Any) -> Expression

Checks if a value is a DateTime. Must be called in a rule or query context. To check if a value is a date, use Date().

Parameters#

NameTypeDescription
valAnyThe value to check.

Returns#

An Expression that filters DateTime values.

Example#

Use DateTime() to check if a value is a DateTime:

#import relationalai as rai
from relationalai.std.dates import DateTime, datetime

# Create a model named "people" with a `Person` type.
model = rai.Model("people")
Person = model.Type("Person")

# Add people to the model.
with model.rule():
    # Add a person with a birth date as a string.
    Person.add(name="Alice", born_at="2021-01-01T12:00:00")
    # Add a person with a birth date as a DateTime.
    Person.add(name="Bob", born_at=datetime(2021, 1, 1, 12, 0, 0))

# Get names and birth dates of people, provided the
# .born_at property is a DateTime value.
with model.query() as select:
    person = Person()
    DateTime(person.born_at)  # Filter DateTime values.
    response = select(person.name, person.born_at)

# Only people where 'born_at' is a DateTime are returned.
print(response.results)
# Output:
#   name                   born_at
# 0  Bob  2021-01-01 12:00:00+00:00

If val produces no DateTime values, DateTime(val) will not filter any values, possibly resulting in an empty query result:

#import relationalai as rai
from relationalai.std.dates import DateTime

# Create a model named "people" with a `Person` type.
model = rai.Model("people")
Person = model.Type("Person")

# Add a person to the model.
with model.rule():
    Person.add(name="Bob", age=30)

with model.query() as select:
    person = Person()
    DateTime(person.age)
    response = select(person.name)

print(response.results)
# Output:
# Empty DataFrame
# Columns: []
# Index: []

DateTime(person.born_at) does not produce True or False values. Instead, it filters the values produced by person.born_at to only include DateTime values. If you need a True or False value, you can use a Model.match() context:

#import relationalai as rai
from relationalai.std.dates import DateTime, datetime

# Create a model named "people" with a `Person` type.
model = rai.Model("people")
Person = model.Type("Person")

# Add some people to the model.
with model.rule():
    Person.add(name="Alice", born_at="2021-01-01T12:00:00")  # born_at is a string.
    Person.add(name="Bob", born_at=datetime(2021, 1, 1, 12, 0, 0))  # born_at is a datetime.

# Check if born_at is a datetime.
with model.query() as select:
    person = Person()
    with model.match() as matched:
        # If person.born_at is a DateTime, add True to matched.
        with model.case():
            DateTime(person.born_at)
            matched.add(person, is_datetime=True)
        # Otherwise, add False.
        with model.case():
            matched.add(person, is_datetime=False)
    response = select(person.name, person.born_at, matched.is_datetime)

print(response.results)
# Output:
#     name              born_at  is_datetime
# 0  Alice  2021-01-01T12:00:00        False
# 1    Bob  2021-01-01T12:00:00         True

See Also#