Skip to main content
Operators and functions for shifting, truncating, extracting from, and formatting the timestamp columns on your data, such as events.requested_at and users.created_at / users.updated_at. Unless noted otherwise, each entry works on both the PostgreSQL and ClickHouse backends.

timestamp ± INTERVAL

Add or subtract a fixed span of time to or from a timestamp using an interval literal. This is the most portable way to express relative time windows. Syntax
Arguments
  • timestamp — the timestamp (or date) to shift.
  • quantity unit — an interval literal such as '30 days', '6 hours', or '1 month'.
Returned value
  • A timestamp shifted forward (for +) or backward (for -) by the interval.
Example
Events in the last 30 days

now

Returns the current date and time. Syntax
Returned value
  • The current timestamp.
Example
Events in the last hour

current_date

Returns the current date, with no time component. Syntax
Returned value
  • The current date.
Example
Users created today

date_diff

Counts the number of unit boundaries crossed between a start and an end timestamp. Syntax
Arguments
  • unit — the unit to count, one of day, hour, minute, second, week, month, quarter, or year.
  • startdate — the earlier timestamp.
  • enddate — the later timestamp.
Returned value
  • The whole number of units between startdate and enddate.
Example
Days since each user signed up

date_trunc

Truncates a timestamp down to the start of the specified unit. Syntax
Arguments
  • unit — the precision to truncate to, one of day, hour, minute, week, month, quarter, or year.
  • timestamp — the timestamp to truncate.
Returned value
  • The timestamp truncated to the start of the given unit.
Example
Event counts by month

extract

Extracts a single field, such as the year or day of week, from a timestamp. Syntax
Arguments
  • field — the field to extract, one of YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, QUARTER, DOW, ISODOW, DOY, WEEK, or EPOCH.
  • timestamp — the timestamp to read from.
Returned value
  • The numeric value of the requested field.
Example
Events grouped by hour of day

toStartOfMinute / toStartOfFiveMinutes / toStartOfTenMinutes / toStartOfFifteenMinutes / toStartOfHour / toStartOfDay

Round a timestamp down to the start of a fixed-length time bucket, from one minute up to a full day. Syntax
Arguments
  • datetime — the timestamp to round.
Returned value
  • The timestamp rounded down to the start of the chosen bucket.
Example
Event volume per 15-minute bucket

toStartOfWeek / toStartOfMonth / toStartOfQuarter / toStartOfYear

Round a timestamp down to the start of a calendar period. toStartOfWeek uses a Monday-start week. Syntax
Arguments
  • datetime — the timestamp to round.
Returned value
  • The date at the start of the enclosing week, month, quarter, or year.
Example
New users per month

toDate

Truncates a timestamp to its date component, dropping the time of day. Syntax
Arguments
  • datetime — the timestamp to truncate.
Returned value
  • The date portion of the input.
Example
Distinct active days per event

timestamp subtraction

Subtracts one timestamp from another to measure the elapsed time between them. Syntax
Arguments
  • timestamp — the two timestamps to subtract; the second is subtracted from the first.
Returned value
  • An interval representing the elapsed time between the two timestamps.
Example
Time between account creation and last update

timestampadd / timestampsub

Add or subtract a whole number of calendar units to or from a timestamp. Equivalent to adding or subtracting an interval, but the unit and count are passed as arguments. Syntax
Arguments
  • unit — the calendar unit to add or subtract, such as day, hour, minute, second, week, month, quarter, or year.
  • count — the whole number of units to add (timestampadd) or subtract (timestampsub).
  • timestamp — the timestamp to shift.
Returned value
  • The shifted timestamp.
Example
Users created within 90 days

add_months

Shifts a date or timestamp by a whole number of months, clamping to the last valid day when the target month is shorter. Syntax
Arguments
  • datetime — the date or timestamp to shift.
  • num_months — the number of months to add; negative values shift backward.
Returned value
  • The date or timestamp moved by num_months months.
Example
Users whose trial converts three months after signup

addDays / addHours / addMinutes / addSeconds / addWeeks / addQuarters / addYears

Add a whole number of a single time unit to a date or timestamp. Each function targets one unit. Syntax
Arguments
  • datetime — the date or timestamp to shift.
  • n — the whole number of units to add.
Returned value
  • The date or timestamp shifted forward by n of the given unit.
Example
Expiry 14 days after an event

subtractDays / subtractHours / subtractMinutes / subtractSeconds / subtractWeeks / subtractMonths / subtractQuarters / subtractYears

Subtract a whole number of a single time unit from a date or timestamp. Each function targets one unit. Syntax
Arguments
  • datetime — the date or timestamp to shift.
  • n — the whole number of units to subtract.
Returned value
  • The date or timestamp shifted backward by n of the given unit.
Example
Events in the last week

at time zone

Reinterprets a timestamp in a named time zone. Applied to a timestamp without time zone, it produces the corresponding instant; applied to a timestamp with time zone, it shows the wall-clock time in that zone. Syntax
Arguments
  • timestamp — the timestamp to reinterpret.
  • zone — an IANA time-zone name such as 'America/New_York' or 'UTC'.
Returned value
  • The timestamp converted to the specified time zone.
Example
Event time in New York local time

to_char

Formats a timestamp into a string using a format template. Syntax
Arguments
  • timestamp — the timestamp to format.
  • format — the format template, for example 'YYYY-MM-DD' or 'HH24:MI:SS'.
Returned value
  • The formatted string.
Example
Format the signup date as a string

make_date

Builds a date from separate year, month, and day fields. Syntax
Arguments
  • year — the year (integer).
  • month — the month, 1 through 12.
  • day — the day of month.
Returned value
  • The constructed date.
Example
Events on or after the start of the fiscal year

toYYYYMM / toYYYYMMDD / toYYYYMMDDhhmmss

Encode a timestamp as a numeric key at month, day, or second precision. Useful for compact grouping and ordering. Syntax
Arguments
  • datetime — the timestamp to encode.
Returned value
  • An integer encoding the timestamp: YYYYMM (for example 202607), YYYYMMDD (for example 20260716), or YYYYMMDDhhmmss.
Example
Events grouped by month key

unix_timestamp

Converts a timestamp to the number of seconds since the Unix epoch. Syntax
Arguments
  • datetime — the timestamp to convert.
Returned value
  • The number of seconds since 1970-01-01 00:00:00 UTC, as an integer.
Example
Epoch seconds for each signup

to_timestamp

Converts a Unix epoch value (seconds since 1970-01-01 00:00:00 UTC) into a timestamp. Syntax
Arguments
  • unix_seconds — the number of seconds since the Unix epoch.
Returned value
  • The corresponding timestamp.
Example
Convert a stored epoch property to a timestamp

dateName

Returns the name of a specified part of a date or timestamp as a string. Syntax
Arguments
  • datepart — the part to name, such as 'year', 'quarter', 'month', 'week', 'dayofyear', 'day', 'weekday', 'hour', 'minute', or 'second'.
  • datetime — the date or timestamp to read from.
Returned value
  • The name of the requested part as a string, for example 'Monday' or 'April'.
Example
Events grouped by weekday name

monthName

Returns the full English name of the month for a date or timestamp. Syntax
Arguments
  • datetime — the date or timestamp to read from.
Returned value
  • The full month name as a string, for example 'January'.
Example
Events grouped by month name

timeSlot

Rounds a timestamp down to the start of its half-hour slot. Syntax
Arguments
  • datetime — the timestamp to round.
Returned value
  • The timestamp rounded down to the nearest 30-minute boundary.
Example
Event volume per half-hour slot

timeZoneOffset

Returns the offset from UTC, in seconds, for a timestamp, accounting for daylight saving time. Syntax
Arguments
  • datetime — the timestamp to evaluate.
Returned value
  • The offset from UTC in seconds.
Example
UTC offset at event time

toISOWeek / toISOYear

Return the ISO 8601 week number and week-numbering year for a timestamp. Under ISO rules, a week belongs to the year that contains its Thursday. Syntax
Arguments
  • datetime — the timestamp to evaluate.
Returned value
  • toISOWeek returns the ISO week number, 1 through 53. toISOYear returns the ISO week-numbering year.
Example
Events grouped by ISO week

toWeek / toYearWeek

Return the week number, and the combined year-and-week, for a timestamp using ISO week numbering. Syntax
Arguments
  • datetime — the timestamp to evaluate.
Returned value
  • toWeek returns the week number. toYearWeek returns an integer combining the year and week as YYYYWW.
Example
Events per year-week

toLastDayOfMonth / toLastDayOfWeek

Return the last day of the calendar period containing a timestamp. toLastDayOfWeek uses a Monday-start week, so it returns the Sunday. Syntax
Arguments
  • datetime — the timestamp to evaluate.
Returned value
  • The date of the last day of the enclosing month or week.
Example
Month-end date for each signup

toUnixTimestamp64Milli

Converts a high-precision timestamp to the number of milliseconds since the Unix epoch. Syntax
Arguments
  • datetime — the timestamp to convert.
Returned value
  • The number of milliseconds since 1970-01-01 00:00:00 UTC, as a 64-bit integer.
Example
Epoch milliseconds for each event

yesterday

Returns the date of the day before today. Syntax
Returned value
  • Yesterday’s date, with no time component.
Example
Events that happened yesterday

from_utc_timestamp / to_utc_timestamp

Convert a timestamp between UTC and a named time zone. from_utc_timestamp reads the input as UTC and returns the wall-clock time in the target zone; to_utc_timestamp reads the input as local to the target zone and returns the equivalent UTC time. Syntax
Arguments
  • timestamp — the timestamp to convert.
  • timezone — an IANA time-zone name such as 'America/New_York'.
Returned value
  • The converted timestamp.
Example
Show event time in the user's local zone

age

Returns the symbolic difference between two timestamps as years, months, and days. With a single argument, the difference is measured from the current date at midnight (PostgreSQL only). Syntax
Arguments
  • timestamp — with two arguments, the second is subtracted from the first. With one argument, the timestamp is subtracted from current_date.
Returned value
  • An interval expressed in years, months, and days.
Example
Account age as years and months