truncate_date

datetime

Truncates a date to the start of the specified unit

Syntax

truncate_date(date, unit)

Parameters

date (date|timestamp)

The date to truncate

unit (string)

The unit to truncate to ("day", "month", "year")

Returns

date

The date truncated to the start of the specified unit

Examples

Truncate to start of day
Input:
truncate_date("2024-01-15", "day")
Output:
"2024-01-15"
Truncate to start of month
Input:
truncate_date("2024-01-15", "month")
Output:
"2024-01-01"
Truncate to start of year
Input:
truncate_date("2024-01-15", "year")
Output:
"2024-01-01"
Get date only
Input:
.timestamp | truncate_date("day")
Output:
Remove time component

The truncate_date() function truncates a date or timestamp to the beginning of the specified unit (day, month, or year), useful for grouping and aggregating by time periods.

Usage

Use truncate_date() to group data by time periods, normalize dates for comparison, remove time components, or prepare data for time-based aggregations.