date_trunc(field, source) — Truncate a timestamp to a given precision
Last updated: 2026-11-04
date_trunc(field, source) — Truncate a timestamp to a given precision
date_trunc() cuts a timestamp down to a chosen unit — day, hour, month — and returns the start of that unit. It is the standard way to group by day or month, generate time series and fill gaps in reports; for timestamps with time zone you can also pass a third argument naming the time zone.
| Kind | function |
|---|---|
| Example Language | SQL |
📝 Syntax
date_trunc(field text, source timestamp) → timestamp
date_trunc(field text, source timestamp with time zone [, zone text]) → timestamp with time zone
⚙️ Parameters
| field text | The truncation unit: microsecond, millisecond, second, minute, hour, day, week, month, quarter, year, decade, century or millennium. Type text. |
|---|---|
| source timestamp | The timestamp to truncate; either a timestamp or a timestamptz. |
| zone text | For timestamptz only: the time zone name used while truncating. Type text. |
Returns:timestamp or timestamp with time zone — the timestamp truncated to the start of that unit.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
day_start | month_start | order_count
---------------------+---------------------+-------------
2024-08-15 00:00:00 | 2024-08-01 00:00:00 | 2
2024-08-17 00:00:00 | 2024-08-01 00:00:00 | 1
(2 rows)
📌 Key Points
- The field is quoted and must be lower case — 'day', 'month' — writing 'DAY' raises an error.
- A timestamp is converted using the session TimeZone setting; a timestamptz is truncated in the session time zone, or in the zone given by the optional third argument.
- date_trunc('week', ts) starts on Monday, matching ISO 8601.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QShould I group by day with date_trunc or with to_char?
APrefer date_trunc(): it keeps the timestamp type so you can keep doing date arithmetic, while to_char() returns text that is only good for display and cannot be used against an index directly.