Dates and times
- Version
- 19
- Review
- Draft
Summary#
timestamptz represents an instant: it is stored in UTC and shown in the session time zone, and the original zone is not kept. interval represents a span with months, days and smaller units.
Syntax#
timestamptz '2026-03-09 09:30+07'
instant AT TIME ZONE 'Area/City'
instant + interval '1 day'
date_trunc('month', instant)
date_bin(interval '15 minutes', instant, origin)
date '2026-03-09' - date '2026-03-01'
Example#
The output below is from PostgreSQL 19.0, produced with psql -X -q -A -P null=NULL -v ON_ERROR_STOP=1 -f example.sql in a scratch database. The script rolls back at the end, so it leaves nothing behind.
BEGIN;
SET LOCAL TimeZone = 'UTC';
SET LOCAL DateStyle = 'ISO, YMD';
SET LOCAL IntervalStyle = 'postgres';
CREATE TEMPORARY TABLE meetings (
meeting_id integer PRIMARY KEY,
starts_at timestamptz NOT NULL,
duration interval NOT NULL
);
INSERT INTO meetings VALUES
(1, '2026-03-07 12:00 America/New_York', '45 minutes'),
(2, '2026-03-09 09:30+07', '1 hour 30 minutes');
SELECT meeting_id,
starts_at,
starts_at + duration AS ends_at,
starts_at AT TIME ZONE 'Asia/Ho_Chi_Minh' AS local_start
FROM meetings
ORDER BY meeting_id;
-- New York moves its clocks forward on 2026-03-08.
SET LOCAL TimeZone = 'America/New_York';
SELECT starts_at + interval '1 day' AS plus_1_day,
starts_at + interval '24 hours' AS plus_24_hours
FROM meetings
WHERE meeting_id = 1;
SET LOCAL TimeZone = 'UTC';
SELECT date '2026-01-31' + interval '1 month' AS month_end,
date '2026-03-01' - date '2026-01-01' AS days_between,
date_trunc('month', timestamptz '2026-03-09 02:30+00') AS month_start,
date_bin('15 minutes',
timestamptz '2026-03-09 02:38+00',
timestamptz '2026-01-01 00:00+00') AS quarter_hour;
-- A half-open range includes the whole of the last day.
SELECT meeting_id
FROM meetings
WHERE starts_at >= timestamptz '2026-03-09 00:00+00'
AND starts_at < timestamptz '2026-03-10 00:00+00'
ORDER BY meeting_id;
ROLLBACK;
Expected output#
meeting_id|starts_at|ends_at|local_start
1|2026-03-07 17:00:00+00|2026-03-07 17:45:00+00|2026-03-08 00:00:00
2|2026-03-09 02:30:00+00|2026-03-09 04:00:00+00|2026-03-09 09:30:00
(2 rows)
plus_1_day|plus_24_hours
2026-03-08 12:00:00-04|2026-03-08 13:00:00-04
(1 row)
month_end|days_between|month_start|quarter_hour
2026-02-28 00:00:00|59|2026-03-01 00:00:00+00|2026-03-09 02:30:00+00
(1 row)
meeting_id
2
(1 row)
Common mistakes#
BETWEEN '2026-03-09' AND '2026-03-09' on a timestamp column covers only midnight. Use a half-open range as above. In a text zone such as AT TIME ZONE '+07', the sign follows POSIX rules and means seven hours west of Greenwich. Prefer named zones such as Asia/Ho_Chi_Minh. Across a daylight-saving transition, adding one calendar day can differ from adding 24 hours.
Version applicability#
date_bin requires PostgreSQL 14. Use a positive stride without month or year units. The other operations work on earlier versions. Named zone results follow the server's time zone data.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0