strftime()#
relationalai.std.dates
#strftime(date: date | datetime | Producer, format: str | Producer) -> Expression
Formats a date or datetime value as a string according to format
.
Format codes are different from Python’s datetime.strptime()
function.
See Format Codes for details.
Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
date | date , datetime , or Producer | The date or datetime value to format as a string. |
format | str or Producer | The format string to format the date or datetime value. See Format Codes for supported format codes. |
Returns#
An Expression
object.
Example#
Use strftime()
to format a date or datetime value as a string according to a format string.
Common ISO format strings are available in the ISO
namespace:
#import relationalai as rai
from relationalai.std.dates import datetime, ISO, strftime
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Person = model.Type("Person")
with model.rule():
Person.add(id=1).set(name="Alice", birthdate=datetime(2001, 1, 1, 13))
# =======
# EXAMPLE
# =======
with model.query() as select:
person = Person()
# Format the person.birthdate property as an ISO date string (YYYY-MM-DD).
birthdate_fmt1 = strftime(person.birthdate, ISO.DATE)
# Format the birthdate using custom format codes.
birthdate_fmt2 = strftime(person.birthdate, "e U d, Y")
response = select(person.name, birthdate_fmt1, birthdate_fmt2)
print(response.results)
# name v v2
# 0 Alice 2001-01-01 Mon January 1, 2001
See Format Codes for details on the format codes used in RAI format strings.