Sub topic 9
Adding and subtracting dates and timestamps
This chapter covers arithmetic on date and time values: which operators exist, what type each returns, and where the results surprise people. It assumes you have read the earlier chapters on the temporal types, timestamptz, the session TimeZone setting, and intervals. It also assumes you know that interval values hold separate month, day, and time fields.
The operator table
PostgreSQL defines + and - for most combinations of date and time types. The result type depends on the operands. It is not always the type of the left operand.
| Expression | Result type | Meaning |
|---|---|---|
| `date + integer` | `date` | Move forward by whole days |
| `date - integer` | `date` | Move backward by whole days |
| `date - date` | `integer` | Number of days between them |
| `date + interval` | `timestamp` | Midnight of the date, shifted by the interval |
| `date + time` | `timestamp` | Combine a date and a time of day |
| `timestamp + interval` | `timestamp` | Shift a wall-clock time |
| `timestamptz + interval` | `timestamptz` | Shift an instant, using the session zone for day and month units |
| `timestamp - timestamp` | `interval` | Days plus a time part |
| `timestamptz - timestamptz` | `interval` | Days plus a time part, from elapsed time |
| `time + interval` | `time` | Wraps around at midnight |
| `interval * number` | `interval` | Scale every field |
Two results are easy to miss. First, date + interval returns a timestamp, not a date. Second, there is no date + date or timestamp + timestamp, because adding two points in time has no meaning.
Date arithmetic with integers
Dates behave like day counters. Adding an integer moves forward by that many days, and subtracting two dates gives a plain integer.
SELECT date '2024-02-28' + 2 AS plus_two_days,
date '2024-03-01' - 1 AS minus_one_day,
date '2024-03-01' - date '2024-01-01' AS days_between;
-- plus_two_days: 2024-03-01
-- minus_one_day: 2024-02-29
-- days_between: 60The integer form is the cleanest way to count calendar days, because it has no time zone and no DST. If your columns are timestamptz, you can still use it by casting to date. That cast uses the session TimeZone, so set it first or convert explicitly with AT TIME ZONE.
Timestamp differences
Subtracting two timestamps returns an interval. The result is built from elapsed time and then normalized so that each 24 hours becomes one day. It never contains months or years.
SELECT timestamp '2024-03-01 00:00' - timestamp '2024-01-01 00:00' AS gap;
-- 60 days
SELECT timestamp '2024-03-01 18:30' - timestamp '2024-03-01 08:00' AS gap;
-- 10:30:00Three tools cover most reporting needs:
extract(epoch FROM b - a)returns the total elapsed seconds as a number. Use it for durations.age(b, a)returns a symbolic result in years, months, and days, such as2 mons 5 days. Use it for human-readable ages.b::date - a::datereturns whole calendar days, ignoring the time of day.
SELECT extract(epoch FROM timestamp '2024-03-01 18:30' - timestamp '2024-03-01 08:00') AS seconds,
age(date '2024-03-15', date '2024-01-10') AS symbolic_age;
-- seconds: 37800
-- symbolic_age: 2 mons 5 daysAdding intervals: months, days, and hours differ
An interval keeps months, days, and a time part as separate fields, and each one is applied differently.
- Months move the calendar month. If the target month is shorter, the day is clamped to its last day.
- Days move the calendar date. The time of day stays the same on the local clock.
- Hours and smaller units add exactly that much elapsed time.
SELECT date '2024-01-31' + interval '1 month' AS month_end_add;
-- 2024-02-29 00:00:00Because of clamping, month arithmetic is not reversible or associative.
SELECT date '2024-01-31' + interval '1 month' + interval '1 month' AS stepwise,
date '2024-01-31' + interval '2 months' AS at_once;
-- stepwise: 2024-03-29 00:00:00
-- at_once: 2024-03-31 00:00:00The stepwise version loses the 31st at the first step and never gets it back. When you need "same day next month" for many periods, compute each target from the original date (start + n * interval '1 month') instead of chaining additions.
Intervals also scale and combine:
SELECT interval '1 day' * 7 AS one_week,
interval '90 minutes' / 2 AS half,
time '23:00' + interval '2 hours' AS wrapped,
date '2024-06-01' + time '09:30' AS combined;
-- one_week: 7 days
-- half: 00:45:00
-- wrapped: 01:00:00
-- combined: 2024-06-01 09:30:00The time + interval result wraps silently and loses the day, so use timestamp whenever the sum can cross midnight.
Arithmetic on timestamptz and DST
Plain timestamp has no zone, so a day is always a day. With timestamptz, PostgreSQL does the arithmetic in the session TimeZone. A day unit means "same local clock time on the next calendar date", while an hours unit means elapsed time. Across a DST change those two differ.
SET TimeZone = 'America/New_York';
SELECT TIMESTAMPTZ '2024-03-09 12:00:00' + interval '1 day' AS plus_one_day,
TIMESTAMPTZ '2024-03-09 12:00:00' + interval '24 hours' AS plus_24_hours;
-- plus_one_day: 2024-03-10 12:00:00-04
-- plus_24_hours: 2024-03-10 13:00:00-04The US clocks moved forward on 2024-03-10, so that calendar day lasted only 23 hours. Adding 1 day kept noon. Adding 24 hours landed an hour later on the wall clock.
Subtraction goes the other way. The difference between two timestamptz values is computed from elapsed time, and then each 24 hours is relabeled as a day:
SET TimeZone = 'America/New_York';
SELECT TIMESTAMPTZ '2024-03-11 12:00:00' - TIMESTAMPTZ '2024-03-09 12:00:00' AS gap;
-- 1 day 23:00:00The two timestamps are two calendar days apart on the clock, but only 47 hours apart in real time. The result reports the 47 hours as 1 day 23:00:00. Adding that interval back to the earlier value would not reproduce the later value, because the 1 day part is applied on the local calendar. Treat an interval from subtraction as a duration, not as a calendar offset.
Results that depend on the session zone
The same timestamptz + interval '1 day' expression gives different instants under different TimeZone settings, as long as the zone has DST in the range. In UTC the sum above would be exactly 24 hours apart. This makes code that is correct in one session wrong in another. There are two ways to avoid the problem:
- Set
TimeZoneexplicitly at the start of the session or transaction (SET LOCAL TimeZone = '...'). - Do the arithmetic on a plain
timestampin an explicit zone, then convert back.
SET TimeZone = 'UTC';
SELECT ((TIMESTAMPTZ '2024-03-09 17:00:00+00' AT TIME ZONE 'America/New_York')
+ interval '1 day')
AT TIME ZONE 'America/New_York' AS next_local_day;
-- 2024-03-10 16:00:00+00The inner AT TIME ZONE gives New York wall-clock noon on 2024-03-09 as a timestamp. The + interval '1 day' moves it to noon on 03-10. The outer AT TIME ZONE turns it back into an instant. That instant is 16:00 UTC because New York is on EDT (UTC-4) by then. The result no longer depends on the session zone.
Nonexistent local times
Adding a day can land on a clock time that does not exist because of a spring-forward gap. PostgreSQL resolves it by using the offset from before the gap, which moves the result forward.
SET TimeZone = 'America/New_York';
SELECT TIMESTAMPTZ '2024-03-09 02:30:00' + interval '1 day' AS into_gap;
-- 2024-03-10 03:30:00-04The reverse case, a repeated hour in autumn, is ambiguous. PostgreSQL picks one of the two occurrences for you, so never rely on a local time between 01:00 and 02:00 on that night.
Choosing the right approach
Pick the arithmetic by what the business rule means:
- "N calendar days later, same local time" (appointments, subscriptions, deadlines): use a
dayormonthinterval in a fixed, explicit zone. - "N hours of elapsed time later" (timeouts, expiry of a token, SLA clocks): use an
hoursorminutesinterval on atimestamptz. - "How many days between two dates": subtract
datevalues. - "How long did this take":
extract(epoch FROM end - start).
Common mistakes
- Expecting
date + intervalto return adate. It returns atimestamp, so cast with::dateif you need a date. - Chaining month additions and expecting end-of-month dates to survive. Add
nmonths to the original value instead. - Subtracting
timestamptzvalues and reusing the interval as a calendar offset. - Using
time + intervalacross midnight and losing the date. - Running day arithmetic on
timestamptzwith an unset or inherited sessionTimeZone.
Exercise
In the America/New_York session zone, what are the results of TIMESTAMPTZ '2024-11-02 12:00:00' + interval '1 day' and TIMESTAMPTZ '2024-11-02 12:00:00' + interval '24 hours'? US clocks moved back on 2024-11-03, so that day lasted 25 hours. Which one keeps noon on the local clock?
Show answer
The 1 day version returns 2024-11-03 12:00:00-05, which keeps noon on the local clock because the day unit moves the calendar date. The 24 hours version returns 2024-11-03 11:00:00-05, because exactly 24 hours of elapsed time lands an hour earlier on the wall clock after the fall-back change.
Exercise
You store subscription start dates in a date column named started_on. Write an expression for the renewal date that is n months after the start without losing the 31st after a short month, and say what type it returns.
Show answer
Use started_on + n * interval '1 month', for example date '2024-01-31' + 2 * interval '1 month', which gives 2024-03-31 00:00:00. It returns a timestamp, so append ::date to get a date. Computing from the original start avoids the clamping that chained additions accumulate.