Date Functions¶
Functions for parsing dates, retrieving the current date/time, extracting date components, detecting period boundaries, converting between formats, and performing date arithmetic.
Date strings use the YYYYMMDD format (e.g. '20230731'). DateTime strings include time as 'YYYYMMDD HH:MM:SS'. Functions return dates in YYYYMMDD string format unless noted otherwise.
Shared Parameters¶
Many boundary and period functions accept two optional parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
bdays |
bool |
False |
When True, only business days are considered. |
calendar |
str \| None |
None |
Named holiday calendar (e.g. 'ANBIMA', 'BR'). None means no holidays are excluded. |
Casting¶
cast_date¶
Vectorized. Parses value into a date string. Returns default if parsing fails. When only_date=True, strips any time component from the result.
expression_engine.solve('cast_date("20230101")', {})
# '20230101'
expression_engine.solve('cast_date("20230731 14:30:00")', {})
# '20230731 14:30:00'
expression_engine.solve('cast_date("invalid")', {})
# None
import numpy as np
expression_engine.solve('cast_date(dates)', {
'dates': np.array(['20230101', '20231231', None])
})
# ['20230101', '20231231', None]
Current Date & Time¶
now¶
Returns the current date and time as a DateTime. Pass a timezone string (e.g. 'America/New_York') to get local time.
today¶
Returns the current local date.
time¶
Returns the current time without the date component.
Component Extraction¶
get_year¶
Vectorized. Returns the year as an integer. Returns None for invalid dates.
get_quarter¶
Vectorized. Returns the quarter of the year (1–4). Returns None for invalid dates.
get_month¶
Vectorized. Returns the month number (1–12). Returns None for invalid dates.
get_month_name¶
Vectorized. Returns the full English month name. Returns None for invalid dates.
get_week_of_year¶
Vectorized. Returns the week number within the year (1–53). Returns None for invalid dates.
get_week_of_month¶
Vectorized. Returns the week number within the month (1–5). Returns None for invalid dates.
get_day¶
Vectorized. Returns the day of the month (1–31). Returns None for invalid dates.
get_day_name¶
Vectorized. Returns the full English day name. Returns None for invalid dates.
get_day_of_week¶
Vectorized. Returns the day-of-week index: 0 = Monday, 6 = Sunday. Returns None for invalid dates.
get_day_of_year¶
Vectorized. Returns the day number within the year (1–365/366). Returns None for invalid dates.
get_hour¶
Vectorized. Returns the hour (0–23) from a DateTime string. Returns None for date-only values.
get_minute¶
Vectorized. Returns the minute (0–59) from a DateTime string. Returns None for date-only values.
get_second¶
Vectorized. Returns the second (0–59) from a DateTime string. Returns None for date-only values.
Period Boundaries — Last Day¶
get_last_day_of_week¶
Vectorized. Returns the last day of the week containing string_date.
get_last_day_of_month¶
Vectorized. Returns the last day of the month.
expression_engine.solve('get_last_day_of_month("20230614")', {})
# '20230630'
expression_engine.solve('get_last_day_of_month("20230614", True, "ANBIMA")', {})
# last business day of June 2023 per ANBIMA calendar
get_last_day_of_quarter¶
Vectorized. Returns the last day of the quarter.
get_last_day_of_year¶
Vectorized. Returns the last day of the year.
is_last_day_of_week¶
Vectorized. Returns True if string_date is the last day of its week.
expression_engine.solve('is_last_day_of_week("20230610")', {})
# True
expression_engine.solve('is_last_day_of_week("20230609")', {})
# False
is_last_day_of_month¶
Vectorized. Returns True if string_date is the last day of its month.
expression_engine.solve('is_last_day_of_month("20230930")', {})
# True
expression_engine.solve('is_last_day_of_month("20230929")', {})
# False
is_last_day_of_quarter¶
Vectorized. Returns True if string_date is the last day of its quarter.
is_last_day_of_year¶
Vectorized. Returns True if string_date is the last day of its year.
Period Boundaries — First Day¶
get_first_day_of_week¶
Vectorized. Returns the first day of the week containing string_date.
get_first_day_of_month¶
Vectorized. Returns the first day of the month.
get_first_day_of_quarter¶
Vectorized. Returns the first day of the quarter.
get_first_day_of_year¶
Vectorized. Returns the first day of the year.
is_first_day_of_week¶
Vectorized. Returns True if string_date is the first day of its week.
is_first_day_of_month¶
Vectorized. Returns True if string_date is the first day of its month.
is_first_day_of_quarter¶
Vectorized. Returns True if string_date is the first day of its quarter. With bdays=True, checks for the first business day of the quarter.
expression_engine.solve('is_first_day_of_quarter("20230101")', {})
# True
expression_engine.solve('is_first_day_of_quarter("20230403", True)', {})
# True (first business day of Q2 2023; April 1 and 2 were weekend)
is_first_day_of_year¶
Vectorized. Returns True if string_date is the first day of its year.
expression_engine.solve('is_first_day_of_year("20230101")', {})
# True
expression_engine.solve('is_first_day_of_year("20230102")', {})
# False
Format Conversion¶
from_iso_format¶
Vectorized. Parses an ISO 8601 string ('YYYY-MM-DDTHH:MM:SS') into a date string. Returns None on failure.
expression_engine.solve('from_iso_format("2023-06-30T00:00:00")', {})
# '20230630'
expression_engine.solve('from_iso_format("2023-06-30T12:30:45", True)', {})
# '20230630 12:30:45'
to_iso_format¶
Vectorized. Converts a YYYYMMDD date string to ISO format 'YYYY-MM-DD'. Returns None on failure.
from_timestamp¶
Vectorized. Converts a Unix timestamp (seconds since epoch) to a date string.
expression_engine.solve('from_timestamp(1677776000)', {})
# '20230302'
expression_engine.solve('from_timestamp(1677776015, True)', {})
# '20230302 16:53:35'
to_timestamp¶
Vectorized. Converts a YYYYMMDD date string to a Unix timestamp.
from_ordinal_days¶
Vectorized. Converts an ordinal day number (days since 0001-01-01) to a YYYYMMDD string.
to_ordinal_days¶
Vectorized. Converts a YYYYMMDD date string to an ordinal day number.
from_date_format¶
Vectorized. Parses a date string using a custom strptime-style format.
expression_engine.solve('from_date_format("2023-06-30", "%Y-%m-%d")', {})
# '20230630'
expression_engine.solve('from_date_format("2023/06/30 12:30:45", "%Y/%m/%d %H:%M:%S", True)', {})
# '20230630 12:30:45'
to_date_format¶
Vectorized. Formats a date using a custom strftime-style format. Returns None on failure.
expression_engine.solve('to_date_format("20230630", "%Y/%m/%d")', {})
# '2023/06/30'
expression_engine.solve('to_date_format("20230630", "%B %d, %Y")', {})
# 'June 30, 2023'
Date Arithmetic¶
date_delta¶
Shifts string_date by period units of periodicity. Supports scalar and array inputs for both arguments.
periodicity |
Unit |
|---|---|
'D' |
Calendar days |
'B' |
Business days |
'M' |
Months |
'Y' |
Years |
expression_engine.solve('date_delta("20230630", 5)', {})
# '20230705'
expression_engine.solve('date_delta("20230630", -1, "M")', {})
# '20230530'
import numpy as np
expression_engine.solve('date_delta("20230101", periods, "M")', {
'periods': np.array([1, 2, 3])
})
# ['20230201', '20230301', '20230401']
dates_diff¶
Returns the difference between start_date and end_date measured in periodicity units. Supports arrays for both arguments.
expression_engine.solve('dates_diff("20230614", "20230618")', {})
# 4
expression_engine.solve('dates_diff("20230101", "20231231", "M")', {})
# 11
Business Day Navigation¶
is_bday¶
Vectorized. Returns True if string_date is a business day. Returns None for invalid dates.
expression_engine.solve('is_bday("20241111")', {})
# True (Monday)
expression_engine.solve('is_bday("20241109")', {})
# False (Saturday)
previous_bizday¶
Vectorized. Returns the business day immediately before date.
expression_engine.solve('previous_bizday("20241111")', {})
# '20241108' (Friday before Monday Nov 11)
next_bizday¶
Vectorized. Returns the business day immediately after date.
nth_bizday¶
Vectorized. Returns the nth business day of the given month and year. day is an ordinal string such as '1st', '3rd', '15th'.
expression_engine.solve('nth_bizday("15th", 11, 2024)', {})
# '20241121'
expression_engine.solve('nth_bizday("15th", 11, 2024, "ANBIMA")', {})
# '20241125' (adjusted for ANBIMA holidays)
nth_normal_day¶
Vectorized. Returns the nth calendar day of the given month and year.
bizday_after_specific_day¶
Vectorized. Returns the nth business day after the calendar day of the month given by date. bizday_position is 'first', 'second', or 'third'.
expression_engine.solve('bizday_after_specific_day("first", "20241101")', {})
# '20241104' (first bday after day 1 of Nov 2024)
expression_engine.solve('bizday_after_specific_day("first", "20241110")', {})
# '20241111' (first bday after day 10 of Nov 2024)
bizday_before_specific_day¶
Vectorized. Returns the nth business day before the calendar day of the month given by date.
expression_engine.solve('bizday_before_specific_day("first", "20241120")', {})
# '20241119' (first bday before day 20 of Nov 2024)
bizday_after_specific_weekday¶
Vectorized. Returns the nth occurrence of a specific business weekday after the day given by date. weekday is a 3-letter abbreviation: 'Mon', 'Tue', 'Wed', 'Thu', 'Fri'.
expression_engine.solve('bizday_after_specific_weekday("first", "Wed", "20241101")', {})
# '20241106' (first Wednesday after day 1 of Nov 2024)
bizday_before_specific_weekday¶
Vectorized. Returns the nth occurrence of a specific business weekday before the day given by date.