Date & Time Methods
CXL provides 13 built-in methods for date and time manipulation. These methods work on Date and DateTime values. All return null when the receiver is null.
Component extraction
year() -> Int
Returns the year component.
$ cxl eval -e 'emit result = #2024-03-15#.year()'
{
"result": 2024
}
month() -> Int
Returns the month component (1-12).
$ cxl eval -e 'emit result = #2024-03-15#.month()'
{
"result": 3
}
day() -> Int
Returns the day-of-month component (1-31).
$ cxl eval -e 'emit result = #2024-03-15#.day()'
{
"result": 15
}
hour() -> Int
Returns the hour component (0-23). DateTime only – returns null for Date values.
$ cxl eval -e 'emit result = "2024-03-15T14:30:00".to_datetime().hour()'
{
"result": 14
}
minute() -> Int
Returns the minute component (0-59). DateTime only – returns null for Date values.
$ cxl eval -e 'emit result = "2024-03-15T14:30:00".to_datetime().minute()'
{
"result": 30
}
second() -> Int
Returns the second component (0-59). DateTime only – returns null for Date values.
$ cxl eval -e 'emit result = "2024-03-15T14:30:45".to_datetime().second()'
{
"result": 45
}
Date arithmetic
add_days(n: Int) -> Date
Adds n days to the date. Use negative values to subtract. Works on both Date and DateTime.
$ cxl eval -e 'emit result = #2024-01-15#.add_days(10)'
{
"result": "2024-01-25"
}
$ cxl eval -e 'emit result = #2024-01-15#.add_days(-5)'
{
"result": "2024-01-10"
}
add_months(n: Int) -> Date
Adds n months to the date. Day is clamped to the last day of the target month if necessary.
$ cxl eval -e 'emit result = #2024-01-31#.add_months(1)'
{
"result": "2024-02-29"
}
$ cxl eval -e 'emit result = #2024-03-15#.add_months(-2)'
{
"result": "2024-01-15"
}
add_years(n: Int) -> Date
Adds n years to the date. Leap day (Feb 29) is clamped to Feb 28 in non-leap years.
$ cxl eval -e 'emit result = #2024-02-29#.add_years(1)'
{
"result": "2025-02-28"
}
Date difference
diff_days(other: Date) -> Int
Returns the number of days between the receiver and the argument (receiver - other). Positive when the receiver is later.
$ cxl eval -e 'emit result = #2024-03-15#.diff_days(#2024-03-01#)'
{
"result": 14
}
$ cxl eval -e 'emit result = #2024-01-01#.diff_days(#2024-03-15#)'
{
"result": -74
}
diff_months(other: Date) -> Int
Returns the difference in months between two dates.
Note: This method currently returns
null(unimplemented). Usediff_daysand divide by 30 as an approximation.
diff_years(other: Date) -> Int
Returns the difference in years between two dates.
Note: This method currently returns
null(unimplemented). Usediff_daysand divide by 365 as an approximation.
Formatting
format_date(format: String) -> String
Formats the date/datetime using a chrono format string. See chrono format syntax.
Common format specifiers:
| Specifier | Description | Example |
|---|---|---|
%Y | 4-digit year | 2024 |
%m | 2-digit month | 03 |
%d | 2-digit day | 15 |
%H | Hour (24h) | 14 |
%M | Minute | 30 |
%S | Second | 00 |
%B | Full month name | March |
%b | Abbreviated month | Mar |
%A | Full weekday | Friday |
$ cxl eval -e 'emit result = #2024-03-15#.format_date("%B %d, %Y")'
{
"result": "March 15, 2024"
}
$ cxl eval -e 'emit result = #2024-03-15#.format_date("%Y/%m/%d")'
{
"result": "2024/03/15"
}
Practical examples
Fiscal year calculation (April start):
let d = invoice_date
emit fiscal_year = if d.month() < 4 then d.year() - 1 else d.year()
Age in days:
emit days_since = now.diff_days(created_date)
Quarter:
emit quarter = match {
invoice_date.month() <= 3 => "Q1",
invoice_date.month() <= 6 => "Q2",
invoice_date.month() <= 9 => "Q3",
_ => "Q4"
}
ISO week format:
emit formatted = order_date.format_date("%Y-W%V")