date
date(year: IntegerValue, month: IntegerValue, day: IntegerValue)Construct a date from year, month, and day.
When you construct a date using this class, it returns an Expression and not a date
object. All of the methods on this class are class methods that operate on date
expressions.
Parameters
Section titled “Parameters”
(yearIntegerValue) - The year.
(monthIntegerValue) - The month (1-12).
(dayIntegerValue) - The day of the month.
Examples
Section titled “Examples”Construct dates:
>>> datetime.date(2024, 1, 15)>>> datetime.date(Employee.hire_year, Employee.hire_month, Employee.hire_day)Methods
Section titled “Methods”.year()
Section titled “.year()”date.year(date: DateValue) -> ExpressionExtract the year from a date.
Parameters:
(dateDateValue) - The date to extract from.
Returns:
Expression- AnExpressioncomputing the year. ReturnsInteger.
Examples:
Extract year from dates:
>>> datetime.date.year(Event.start_date)>>> select(Person.name).where(datetime.date.year(Person.birth_date) == 2000).quarter()
Section titled “.quarter()”date.quarter(date: DateValue) -> ExpressionExtract the quarter (1-4) from a date.
Parameters:
(dateDateValue) - The date to extract from.
Returns:
Expression- AnExpressioncomputing the quarter. ReturnsInteger.
.month()
Section titled “.month()”date.month(date: DateValue) -> ExpressionExtract the month (1-12) from a date.
Parameters:
(dateDateValue) - The date to extract from.
Returns:
Expression- AnExpressioncomputing the month. ReturnsInteger.
.week()
Section titled “.week()”date.week(date: DateValue) -> ExpressionExtract the ISO week number from a date.
Parameters:
(dateDateValue) - The date to extract from.
Returns:
Expression- AnExpressioncomputing the week number. ReturnsInteger.
.day()
Section titled “.day()”date.day(date: DateValue) -> ExpressionExtract the day of the month from a date.
Parameters:
(dateDateValue) - The date to extract from.
Returns:
Expression- AnExpressioncomputing the day. ReturnsInteger.
.dayofyear()
Section titled “.dayofyear()”date.dayofyear(date: DateValue) -> ExpressionExtract the day of the year (1-366) from a date.
Parameters:
(dateDateValue) - The date to extract from.
Returns:
Expression- AnExpressioncomputing the day of year. ReturnsInteger.
.isoweekday()
Section titled “.isoweekday()”date.isoweekday(date: DateValue) -> ExpressionReturn the ISO weekday as an integer, where Monday is 1, and Sunday is 7.
Parameters:
(dateDateValue) - The date to extract from.
Returns:
Expression- AnExpressioncomputing the ISO weekday (1=Monday…7=Sunday). ReturnsInteger.
.weekday()
Section titled “.weekday()”date.weekday(date: DateValue) -> ExpressionReturn the weekday as an integer, where Monday is 0, and Sunday is 6.
Parameters:
(dateDateValue) - The date to extract from.
Returns:
Expression- AnExpressioncomputing the weekday (0=Monday…6=Sunday). ReturnsInteger.
.fromordinal()
Section titled “.fromordinal()”date.fromordinal(ordinal: IntegerValue) -> ExpressionConstruct a date from a proleptic Gregorian ordinal.
Parameters:
(ordinalIntegerValue) - The ordinal (day 1 is 0001-01-01).
Returns:
Expression- AnExpressioncomputing the date. ReturnsDate.
.to_datetime()
Section titled “.to_datetime()”date.to_datetime( date: DateValue, hour: int = 0, minute: int = 0, second: int = 0, millisecond: int = 0, tz: str = "UTC",) -> ExpressionConvert a date to a datetime with specified time components.
Parameters:
(dateDateValue) - The date to convert.
(hourint, default:0) - The hour (0-23). Default: 0.
(minuteint, default:0) - The minute (0-59). Default: 0.
(secondint, default:0) - The second (0-59). Default: 0.
(millisecondint, default:0) - The millisecond (0-999). Default: 0.
(tzstr, default:“UTC”) - The timezone string. Default: “UTC”.
Returns:
Expression- AnExpressioncomputing the datetime. ReturnsDateTime.
.format()
Section titled “.format()”date.format(date: DateValue, format: StringValue) -> ExpressionFormat a date as a string.
Parameters:
(dateDateValue) - The date to format.
(formatStringValue) - The format string.
Returns:
Expression- AnExpressioncomputing the formatted date string. ReturnsString.
Referenced By:
RelationalAI Documentation └── Build With RelationalAI └── Understand how PyRel works > Use advanced reasoning > Rules-based reasoning └── Work with strings
.add()
Section titled “.add()”date.add(date: DateValue, period: Variable) -> ExpressionAdd a period to a date.
Parameters:
Returns:
Expression- AnExpressioncomputing the resulting date. ReturnsDate. ReturnsDate.
Examples:
Add periods to dates:
>>> datetime.date.add(Order.order_date, datetime.days(7))>>> datetime.date.add(Project.start_date, datetime.weeks(Contract.duration_weeks))>>> datetime.date.add(Subscription.start_date, datetime.months(12)).subtract()
Section titled “.subtract()”date.subtract(date: DateValue, period: Variable) -> ExpressionSubtract a period from a date.
Parameters:
Returns:
Expression- AnExpressioncomputing the resulting date. ReturnsDate.
.range()
Section titled “.range()”date.range( start: DateValue | None = None, end: DateValue | None = None, periods: IntegerValue = 1, freq: Frequency = "D",) -> VariableGenerate a range of dates.
Parameters:
(startDateValue|None, default:None) - The start date. If None, computes from end.
(endDateValue|None, default:None) - The end date (inclusive). If None, uses periods.
(periodsIntegerValue, default:1) - Number of periods to generate. Default: 1.
(freqFrequency, default:“D”) - Frequency: “D” (day), “W” (week), “M” (month), “Y” (year). Default: “D”.
Returns:
Variable- AVariablerepresenting the date range. ReturnsDate.
Raises:
ValueError- If neither start nor end is provided, or if freq is invalid.
.period_days()
Section titled “.period_days()”date.period_days(start: DateValue, end: DateValue) -> ExpressionCompute the number of days between two dates.
Returns:
Expression- AnExpressioncomputing the number of days. ReturnsInteger.
.diff()
Section titled “.diff()”date.diff(part: DatePart, start: DateValue, end: DateValue) -> ExpressionCalculate the difference between two dates in the specified date part units.
Parameters:
Returns:
Expression- AnExpressioncomputing the difference. ReturnsInteger.
Examples:
Calculate date differences:
>>> datetime.date.diff("day", Order.start_date, Order.end_date)>>> datetime.date.diff("month", Employee.hire_date, Employee.termination_date)>>> datetime.date.diff("week", Project.start, Project.finish).fromisoformat()
Section titled “.fromisoformat()”date.fromisoformat(date_string: StringValue) -> ExpressionParse a date from ISO format string (yyyy-mm-dd).
Parameters:
(date_stringStringValue) - The ISO format date string.
Returns:
Expression- AnExpressioncomputing the parsed date. ReturnsDate.
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
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