Sub topic 7
Dates, Times, Timestamps and Intervals
This chapter assumes you can write basic SELECT, INSERT and CREATE TABLE statements. It also assumes you have met casting with ::. It covers the PostgreSQL date and time types, the difference between timestamp and timestamptz, time zone conversion, and interval arithmetic.
The types at a glance
| Type | Stores | Storage | Example literal |
|---|---|---|---|
| `date` | Calendar day | 4 bytes | `'2026-01-15'` |
| `time` | Time of day, no zone | 8 bytes | `'14:30:00'` |
| `timestamp` | Date and time, no zone | 8 bytes | `'2026-01-15 14:30:00'` |
| `timestamptz` | A single instant in time | 8 bytes | `'2026-01-15 14:30:00+00'` |
| `interval` | A span of time | 16 bytes | `'1 month 2 days'` |
timestamp and timestamptz have microsecond precision by default. You can reduce it with a modifier, for example timestamptz(0) for whole seconds. PostgreSQL also has timetz, but its own documentation discourages it. A time of day with an offset but no date cannot account for daylight saving rules, so avoid it.
Use ISO 8601 input (YYYY-MM-DD HH:MM:SS). Formats such as 01/02/2026 are read according to the DateStyle setting, which can make them mean January 2 or February 1.
timestamp versus timestamptz
The name timestamptz is misleading. It does not store a time zone. It stores one instant, normalized to UTC. Two things happen at the boundary:
- On input, PostgreSQL uses an offset in the literal if there is one. Otherwise it uses the session's
TimeZonesetting. It then converts to UTC. - On output, PostgreSQL renders the instant in the session's
TimeZone.
SET TimeZone = 'UTC';
SELECT '2026-01-15 12:00:00+00'::timestamptz AS utc_view;
SET TimeZone = 'America/New_York';
SELECT '2026-01-15 12:00:00+00'::timestamptz AS ny_view;The first query returns 2026-01-15 12:00:00+00 and the second returns 2026-01-15 07:00:00-05. The stored instant is the same in both. Only the display changes.
timestamp (without time zone) stores the wall-clock digits exactly as written and ignores any offset in the literal:
SELECT '2026-01-15 12:00:00+05'::timestamp AS ignored_offset;This returns 2026-01-15 12:00:00. The +05 is silently dropped. This is the main reason to prefer timestamptz for events that happened at a real moment, such as created_at. Use plain timestamp only for values that are intentionally local, such as "the store opens at 09:00 on 2026-12-24 wherever the store is".
Converting between zones with AT TIME ZONE
AT TIME ZONE flips between the two types, and the direction depends on the input type:
timestamptz AT TIME ZONE zonereturns atimestamp, the wall-clock time in that zone.timestamp AT TIME ZONE zonereturns atimestamptz, interpreting the digits as being in that zone.
SELECT '2026-01-15 12:00:00+00'::timestamptz AT TIME ZONE 'America/New_York' AS new_york_local;This returns 2026-01-15 07:00:00 with no offset. Use named zones such as America/New_York, which carry daylight saving rules. Abbreviations such as EST are fixed offsets and do not.
Current time functions
These functions differ in when they take the clock reading:
| Function | Returns | Value is fixed at |
|---|---|---|
| `now()`, `current_timestamp` | `timestamptz` | Start of the transaction |
| `statement_timestamp()` | `timestamptz` | Start of the current statement |
| `clock_timestamp()` | `timestamptz` | The moment of the call |
| `current_date` | `date` | Start of the transaction |
Inside a transaction, now() returns the same value on every call. This is useful for consistent audit columns. It is a trap when you try to measure elapsed time in a long transaction, because you need clock_timestamp() for that.
Intervals
An interval stores three separate fields: months, days and microseconds. PostgreSQL does not convert between them when storing, because the conversion depends on the calendar. A month has 28 to 31 days, and a day can be 23 or 25 hours long where daylight saving changes.
SET TimeZone = 'America/New_York';
SELECT '2026-03-07 12:00:00-05'::timestamptz + interval '1 day' AS plus_1_day,
'2026-03-07 12:00:00-05'::timestamptz + interval '24 hours' AS plus_24_hours;Daylight saving time starts on 2026-03-08 in New York. The results are 2026-03-08 12:00:00-04 for one day (same wall-clock time) and 2026-03-08 13:00:00-04 for 24 hours (exactly 24 hours of elapsed time). Pick the unit that matches what you mean.
Month arithmetic clamps to the end of a shorter month:
SELECT DATE '2026-01-31' + interval '1 month' AS clamped;This returns the timestamp 2026-02-28 00:00:00. Note that adding an interval to a date produces a timestamp, not a date. To get a date back, add an integer number of days instead (DATE '2026-01-31' + 7).
Subtraction and age
date - datereturns an integer number of days.timestamp - timestampandtimestamptz - timestamptzreturn aninterval. That interval contains days and a time part, never months.age(a, b)returns a "symbolic" result in years, months and days, which is better for human-readable ages.
Extracting and truncating
extract(field FROM value) pulls out a part such as year, month, dow or epoch. date_trunc('unit', value) rounds down to the start of a unit.
SELECT extract(year FROM TIMESTAMPTZ '2026-05-10 08:00:00+00') AS yr,
date_trunc('month', TIMESTAMPTZ '2026-05-10 08:00:00+00') AS month_start;For timestamptz input, both functions work in the session time zone. Truncating to a day or month gives different instants for different sessions. For reports, either set the zone explicitly or pass it as the third argument to date_trunc, which is available since PostgreSQL 12.
Filtering by time ranges
Use half-open ranges: inclusive start, exclusive end.
CREATE TABLE audit_log (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
action text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
SELECT count(*)
FROM audit_log
WHERE created_at >= '2026-01-01'
AND created_at < '2026-02-01';BETWEEN '2026-01-01' AND '2026-01-31' is a common mistake. The upper bound is read as midnight at the start of January 31, so it excludes almost all of that day. A half-open range never needs you to guess the last representable instant, and it works for any precision.
Common errors and gotchas
- Casting
timestamptztodateuses the session zone. An instant at 02:00 UTC is still the previous day in New York. Convert withAT TIME ZONEfirst when you need a specific zone. - Comparing a
timestampcolumn to atimestamptzvalue forces a conversion using the session zone. This can prevent index use and give results that change between sessions. - Out-of-range fields such as
'2026-02-30'::dateraisedate/time field value out of range. - Special values
'infinity'and'-infinity'are valid fordateand timestamps. They work as open-ended bounds, but arithmetic on them returns infinity. - Application drivers may convert values to local time. Check that your driver returns
timestamptzas an instant, not as a zone-less string.
Exercise
A table has created_at timestamptz. A row was inserted with the literal '2026-06-01 09:00:00' while the session TimeZone was 'America/New_York'. What UTC instant is stored, and what does a session with TimeZone 'UTC' display?
Show answer
The literal has no offset, so PostgreSQL applies the session zone. New York is on daylight time in June (UTC-4), so the stored instant is 2026-06-01 13:00:00 UTC. A UTC session displays 2026-06-01 13:00:00+00.
Exercise
Write a WHERE clause that selects rows from audit_log created during February 2026, and explain why you did not use BETWEEN.
Show answer
WHERE createdat >= '2026-02-01' AND createdat < '2026-03-01'. This half-open range includes every instant in February, however fine the precision, and excludes midnight on March 1. BETWEEN includes both bounds, so you would have to choose an end value such as '2026-02-28 23:59:59.999999', which is error-prone and wrong in leap years.