Skip to main content

date.truncate

Description

Truncates a date/time value to the specified unit (e.g., day, month, year), setting all smaller units to their minimum values. All operations are UTC-based.

Syntax

flex.date.truncate(datetime, unit)

Parameters

ParameterTypeRequiredDescription
datetimeDate/number/stringYesThe date/time value to truncate
unitstringYesThe unit to truncate to

Supported Units

UnitDescriptionTruncates To
'minute'Truncate to start of minuteSets seconds and milliseconds to 0
'hour'Truncate to start of hourSets minutes, seconds, and milliseconds to 0
'day'Truncate to start of daySets time to 00:00:00.000
'week'Truncate to start of weekSets to Monday 00:00:00.000
'month'Truncate to start of monthSets to 1st day at 00:00:00.000
'quarter'Truncate to start of quarterSets to 1st day of quarter month at 00:00:00.000
'year'Truncate to start of yearSets to January 1st at 00:00:00.000

Returns

Type: Date

A new Date object truncated to the specified unit. Returns null if input is invalid. Returns the original date if unit is unrecognized.

Examples

Example 1: Truncate to Day

WITH datetime('2024-03-15T14:30:45Z') AS dt
RETURN flex.date.truncate(dt, 'day') AS truncated

Output:

truncated
--------------------------
2024-03-15T00:00:00.000Z

Example 2: Truncate to Month

WITH datetime('2024-03-15T14:30:45Z') AS dt
RETURN flex.date.truncate(dt, 'month') AS truncated

Output:

truncated
--------------------------
2024-03-01T00:00:00.000Z

Example 3: Group Events by Week

MATCH (e:Event)
WITH flex.date.truncate(e.timestamp, 'week') AS week, count(*) AS eventCount
RETURN week, eventCount
ORDER BY week

Example 4: Monthly Aggregation

MATCH (s:Sale)
WITH flex.date.truncate(s.date, 'month') AS month, sum(s.amount) AS totalSales
RETURN month, totalSales
ORDER BY month

Example 5: Quarter Analysis

MATCH (o:Order)
WITH flex.date.truncate(o.orderDate, 'quarter') AS quarter, count(*) AS orders
RETURN quarter, orders
ORDER BY quarter DESC

Notes

  • Returns null for invalid date inputs
  • All operations use UTC timezone
  • Week starts on Monday (ISO week convention)
  • Quarters: Q1 (Jan-Mar), Q2 (Apr-Jun), Q3 (Jul-Sep), Q4 (Oct-Dec)
  • Unknown units return the original normalized date
  • Useful for time-series aggregation and bucketing

See Also