datetime
datetime( year: IntegerValue, month: IntegerValue, day: IntegerValue, hour: IntegerValue = 0, minute: IntegerValue = 0, second: IntegerValue = 0, millisecond: IntegerValue = 0, tz: dt.tzinfo | StringValue = "UTC",)Construct a datetime from components.
When you construct a datetime using this class, it returns an Expression and not a
datetime object. All of the methods on this class are class methods that operate on
datetime expressions.
Parameters
Section titled “Parameters”
(yearIntegerValue) - The year.
(monthIntegerValue) - The month (1-12).
(dayIntegerValue) - The day of the month.
(hourIntegerValue, default:0) - The hour (0-23). Default: 0.
(minuteIntegerValue, default:0) - The minute (0-59). Default: 0.
(secondIntegerValue, default:0) - The second (0-59). Default: 0.
(millisecondIntegerValue, default:0) - The millisecond (0-999). Default: 0.
(tzdatetime.tzinfo|StringValue, default:“UTC”) - The timezone. Default: “UTC”.
Examples
Section titled “Examples”Construct datetimes:
>>> datetime.datetime(2024, 1, 15, 14, 30, 0)>>> datetime.datetime(2024, 1, 1, 6, 30, 45, 0, 'America/New_York')>>> datetime.datetime(Event.year, Event.month, Event.day, Event.hour, Event.minute)Methods
Section titled “Methods”.now()
Section titled “.now()”datetime.now() -> ExpressionGet the current datetime.
Returns:
Expression- AnExpressioncomputing the current datetime. ReturnsDateTime.
.year()
Section titled “.year()”datetime.year( datetime: DateTimeValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionExtract the year from a datetime.
Parameters:
(datetimeDateTimeValue) - The datetime to extract from.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for extraction. Default: None (uses datetime’s timezone or UTC).
Returns:
Expression- An Expression computing the year.
.quarter()
Section titled “.quarter()”datetime.quarter( datetime: DateTimeValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionExtract the quarter (1-4) from a datetime.
Parameters:
(datetimeDateTimeValue) - The datetime to extract from.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for extraction. Default: None.
Returns:
Expression- AnExpressioncomputing the quarter. ReturnsInteger.
.month()
Section titled “.month()”datetime.month( datetime: DateTimeValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionExtract the month (1-12) from a datetime.
Parameters:
(datetimeDateTimeValue) - The datetime to extract from.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for extraction. Default: None.
Returns:
Expression- AnExpressioncomputing the month. ReturnsInteger.
.week()
Section titled “.week()”datetime.week( datetime: DateTimeValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionExtract the ISO week number from a datetime.
Parameters:
(datetimeDateTimeValue) - The datetime to extract from.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for extraction. Default: None.
Returns:
Expression- AnExpressioncomputing the week number. ReturnsInteger.
.day()
Section titled “.day()”datetime.day( datetime: DateTimeValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionExtract the day of the month from a datetime.
Parameters:
(datetimeDateTimeValue) - The datetime to extract from.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for extraction. Default: None.
Returns:
Expression- AnExpressioncomputing the day. ReturnsInteger.
.dayofyear()
Section titled “.dayofyear()”datetime.dayofyear( datetime: DateTimeValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionExtract the day of the year (1-366) from a datetime.
Parameters:
(datetimeDateTimeValue) - The datetime to extract from.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for extraction. Default: None.
Returns:
Expression- AnExpressioncomputing the day of year. ReturnsInteger.
.hour()
Section titled “.hour()”datetime.hour( datetime: DateTimeValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionExtract the hour (0-23) from a datetime.
Parameters:
(datetimeDateTimeValue) - The datetime to extract from.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for extraction. Default: None.
Returns:
Expression- AnExpressioncomputing the hour. ReturnsInteger.
.minute()
Section titled “.minute()”datetime.minute( datetime: DateTimeValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionExtract the minute (0-59) from a datetime.
Parameters:
(datetimeDateTimeValue) - The datetime to extract from.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for extraction. Default: None.
Returns:
Expression- AnExpressioncomputing the minute. ReturnsInteger.
.second()
Section titled “.second()”datetime.second(datetime: DateTimeValue) -> ExpressionExtract the second (0-59) from a datetime.
Parameters:
(datetimeDateTimeValue) - The datetime to extract from.
Returns:
Expression- AnExpressioncomputing the second. ReturnsInteger.
.isoweekday()
Section titled “.isoweekday()”datetime.isoweekday( datetime: DateTimeValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionReturn the ISO weekday as an integer, where Monday is 1, and Sunday is 7.
Parameters:
(datetimeDateTimeValue) - The datetime to extract from.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for extraction. Default: None.
Returns:
Expression- AnExpressioncomputing the ISO weekday (1=Monday…7=Sunday). ReturnsInteger.
.weekday()
Section titled “.weekday()”datetime.weekday( datetime: DateTimeValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionReturn the weekday as an integer, where Monday is 0, and Sunday is 6.
Parameters:
(datetimeDateTimeValue) - The datetime to extract from.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for extraction. Default: None.
Returns:
Expression- AnExpressioncomputing the weekday (0=Monday…6=Sunday). ReturnsInteger.
.fromordinal()
Section titled “.fromordinal()”datetime.fromordinal(ordinal: IntegerValue) -> ExpressionConstruct a datetime from a proleptic Gregorian ordinal.
Parameters:
(ordinalIntegerValue) - The ordinal (day 1 is 0001-01-01).
Returns:
Expression- AnExpressioncomputing the datetime at midnight UTC. ReturnsDateTime.
.strptime()
Section titled “.strptime()”datetime.strptime(date_str: StringValue, format: str) -> ExpressionParse a datetime from a string using a format string.
Parameters:
(date_strStringValue) - The datetime string to parse.
(formatstr) - The format string. Must be a literal Python string.
Returns:
Expression- AnExpressioncomputing the parsed datetime. ReturnsDateTime.
Referenced By:
RelationalAI Documentation └── Build With RelationalAI └── Understand how PyRel works > Use advanced reasoning > Rules-based reasoning └── Use dates and datetimes └── Parse dates and datetimes from strings
.to_date()
Section titled “.to_date()”datetime.to_date( datetime: DateTimeValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionConvert a datetime to a date.
Parameters:
(datetimeDateTimeValue) - The datetime to convert.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for conversion. Default: None.
Returns:
Expression- AnExpressioncomputing the date. ReturnsDate.
.format()
Section titled “.format()”datetime.format( date: DateTimeValue, format: StringValue, tz: dt.tzinfo | StringValue | None = None) -> ExpressionFormat a datetime as a string.
Parameters:
(dateDateTimeValue) - The datetime to format.
(formatStringValue) - The format string.
(tzdatetime.tzinfo|StringValue|None, default:None) - The timezone for formatting. Default: None.
Returns:
Expression- AnExpressioncomputing the formatted datetime string. ReturnsString.
Referenced By:
RelationalAI Documentation └── Build With RelationalAI └── Understand how PyRel works > Use advanced reasoning > Rules-based reasoning ├── Work with strings └── Use dates and datetimes └── FormatDateandDateTimevalues as strings
.add()
Section titled “.add()”datetime.add(date: DateTimeValue, period: Variable) -> ExpressionAdd a period to a datetime.
Parameters:
(dateDateTimeValue) - The datetime to add to.
(periodVariable) - The period to add (usemilliseconds(),seconds(),minutes(),hours(),days(), etc.).
Returns:
Expression- AnExpressioncomputing the resulting datetime. ReturnsDateTime. ReturnsDateTime.
Examples:
Add periods to datetimes:
>>> datetime.datetime.add(Event.start_time, datetime.hours(2))>>> datetime.datetime.add(Appointment.scheduled_at, datetime.minutes(30))>>> datetime.datetime.add(Task.created_at, datetime.days(7))Referenced By:
RelationalAI Documentation └── Build With RelationalAI └── Understand how PyRel works > Use advanced reasoning > Rules-based reasoning └── Use dates and datetimes └── Add or subtract time periods
.subtract()
Section titled “.subtract()”datetime.subtract(date: DateTimeValue, period: Variable) -> ExpressionSubtract a period from a datetime.
Parameters:
(dateDateTimeValue) - The datetime to subtract from.
(periodVariable) - The period to subtract (usemilliseconds(),seconds(),minutes(),hours(),days(), etc.).
Returns:
Expression- AnExpressioncomputing the resulting datetime. ReturnsDateTime.
Referenced By:
RelationalAI Documentation └── Build With RelationalAI └── Understand how PyRel works > Use advanced reasoning > Rules-based reasoning └── Use dates and datetimes └── Add or subtract time periods
.range()
Section titled “.range()”datetime.range( start: DateTimeValue | None = None, end: DateTimeValue | None = None, periods: IntegerValue = 1, freq: Frequency = "D",) -> VariableGenerate a range of datetimes.
Parameters:
(startDateTimeValue|None, default:None) - The start datetime. If None, computes from end.
(endDateTimeValue|None, default:None) - The end datetime (inclusive). If None, uses periods.
(periodsIntegerValue, default:1) - Number of periods to generate. Default: 1.
(freqFrequency, default:“D”) - Frequency: “ms”, “s”, “m” (minute), “H”, “D”, “W”, “M”, “Y”. Default: “D”.
Returns:
Variable- AVariablerepresenting the datetime range. ReturnsDateTime.
Raises:
ValueError- If neither start nor end is provided.
.period_milliseconds()
Section titled “.period_milliseconds()”datetime.period_milliseconds(start: DateTimeValue, end: DateTimeValue) -> ExpressionCompute the number of milliseconds between two datetimes.
Parameters:
(startDateTimeValue) - The start datetime.
(endDateTimeValue) - The end datetime.
Returns:
Expression- An Expression computing the number of milliseconds.
Referenced By:
RelationalAI Documentation └── Build With RelationalAI └── Understand how PyRel works > Use advanced reasoning > Rules-based reasoning └── Use dates and datetimes └── Compute elapsed time between two events
.diff()
Section titled “.diff()”datetime.diff(part: DateTimePart, start: DateTimeValue, end: DateTimeValue) -> ExpressionCalculate the difference between two datetimes in the specified datetime part units.
Parameters:
(partDateTimePart) - The unit to measure the difference in: “year”, “quarter”, “month”, “week”, “day”, “hour”, “minute”, “second”, or “millisecond”.
(startDateTimeValue) - The start datetime.
(endDateTimeValue) - The end datetime.
Returns:
Expression- AnExpressioncomputing the difference. ReturnsInteger.
Examples:
Calculate datetime differences:
>>> datetime.datetime.diff("hour", Event.start_time, Event.end_time)>>> datetime.datetime.diff("minute", Task.created_at, Task.completed_at)>>> datetime.datetime.diff("second", Session.login_time, Session.logout_time)Referenced By
Section titled “Referenced By”RelationalAI Documentation └── Build With RelationalAI └── Understand how PyRel works > Use advanced reasoning > Rules-based reasoning ├── Work with strings └── Use dates and datetimes ├── ConstructDateandDateTimevalues ├── Parse dates and datetimes from strings ├── FormatDateandDateTimevalues as strings ├── Add or subtract time periods └── Compute elapsed time between two events