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.

Kindfunction
Example LanguageSQL

📝 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 textThe truncation unit: microsecond, millisecond, second, minute, hour, day, week, month, quarter, year, decade, century or millennium. Type text.
source timestampThe timestamp to truncate; either a timestamp or a timestamptz.
zone textFor 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏