skip to content

dates, times and intervals

date arithmetic, interval math across month ends, and date_trunc for grouping by week.

address
everything.types.dates_and_intervals
part
part 6, types and values
chapter
chapter 4 of 11
reading time
7 min

a date is a day, and date ± integer moves it by days. an interval is three separate counters (months, days and microseconds), so adding one month means "the same day number, next month", clamped to the month's end. that's why january 31 + 1 month is february 28, and + 1 month again is march 28, not 31.

date_trunc('week', ...) starts weeks on monday, the ISO way.

the picture

you know how a subscription that started on january 31 renews on february 28, and then on march 28 forever after? somebody wrote next_renewal = last_renewal + interval '1 month', and every month the 31st got a little further away.

think of an interval as a kitchen timer with three dials: months, days and seconds. adding it to a date turns each dial separately. the month dial says "same day number, next month". there's no february 31, so postgres lands on the last day there is, and the original 31 is forgotten. the next month starts from the 28th.

add =
month-end.sql
  1. 1SELECT d::date, (d + interval '1 month')::date,
  2. 2 (d + interval '1 month' + interval '1 month')::date
  3. 3FROM generate_series(date '2026-01-28', date '2026-01-31', interval '1 day') AS d;
start+ 1 month+ 1 month again

january 28 to 31 all become february 28: the month dial moves, and february has no 29th, 30th or 31st. adding another month starts from the 28th, so all four land on march 28. three days of information, gone in the first step.

real output, postgres 18.6 (PGlite 0.5.12) · dates-and-intervals
the last four days of january, pushed forward. one month at a time, all four collapse onto february 28, and stay together on march 28. two months in one go, each keeps its own day.

the fix for a renewal date is to compute every date from the start, never from the previous one: start + n * interval '1 month'. the clamping then happens once per date, and the 31st comes back whenever the month has one.

sqlempty db
SELECT n, (date '2026-01-31' + n * interval '1 month')::date AS renews_on
FROM generate_series(0, 4) AS n
ORDER BY n;
output, from postgres 18.6
SELECT 5
nint4renews_ondate
02026-01-31
12026-02-28
22026-03-31
32026-04-30
42026-05-31
(5 rows)

february 28, then march 31 again.

dates are days

a date is a calendar day, with no time and no time zone (4 bytes). plus or minus an integer moves it by days, and one date minus another is a number of days:

sqlempty db
SELECT date '2026-01-31' + 1                  AS next_day,
       date '2026-03-01' - date '2026-02-01'  AS days_in_february,
       pg_typeof(date '2026-01-31' + 1)       AS type;
output, from postgres 18.6
SELECT 1
next_daydatedays_in_februaryint4typeregtype
2026-02-0128date
(1 row)

add an interval instead and you leave dates behind:

predict the output
sql
SELECT date '2026-01-31' + interval '1 month';
sqlempty db
SELECT date '2026-01-31' + interval '1 month' AS plus_month,
       pg_typeof(date '2026-01-31' + interval '1 month') AS type;
output, from postgres 18.6
SELECT 1
plus_monthtimestamptyperegtype
2026-02-28 00:00:00timestamp without time zone
(1 row)

the same idea gives you a month's last day: truncate to the first of the month, add a month, step back a day.

sqlempty db
SELECT (date_trunc('month', date '2026-02-17') + interval '1 month - 1 day')::date AS last_of_feb;
output, from postgres 18.6
SELECT 1
last_of_febdate
2026-02-28
(1 row)

an interval is three numbers

postgres stores an interval as months, days and microseconds, kept apart on purpose: a month isn't a fixed number of days, and (across a clock change) a day isn't a fixed number of hours. what you type goes into the field it names:

intervalmonthsdaystimejustified
36:00:000036:00:001 day 12:00:00
1 day 12:00:000112:00:001 day 12:00:00
1 mon1000:00:001 mon
30 days03000:00:001 mon
1 mon -01:00:0010-01:00:0029 days 23:00:00
1 year 2 mons 3 days 04:05:0614304:05:061 year 2 mons 3 days 04:05:06
(6 rows)
real output, postgres 18.6 (PGlite 0.5.12) · dates-and-intervals
six intervals and the three fields postgres keeps for each. 36 hours stays 36 hours, it's never quietly turned into a day. justify_interval does the conversion on request, assuming 24-hour days and 30-day months.

comparison makes the same 30-day, 24-hour assumption, so interval '1 month' = interval '30 days' is true. adding them to a date isn't the same at all:

sqlempty db
SELECT interval '1 month' = interval '30 days'            AS equal,
       timestamp '2026-02-01' + interval '1 month'        AS plus_a_month,
       timestamp '2026-02-01' + interval '30 days'        AS plus_30_days;
output, from postgres 18.6
SELECT 1
equalboolplus_a_monthtimestampplus_30_daystimestamp
t2026-03-01 00:00:002026-03-03 00:00:00
(1 row)

equal intervals, dates two days apart, because february has 28 days. when the length matters, decide which you mean: a calendar month, or 30 days.

age() and subtraction

there are two ways to ask how far apart two days are. subtraction counts days. age() counts in calendar units, years and months first, then whatever days are left:

sqlempty db
SELECT date '2026-03-31' - date '2026-02-28'      AS days,
       age(date '2026-03-31', date '2026-02-28')  AS age;
output, from postgres 18.6
SELECT 1
daysint4ageinterval
311 mon 3 days
(1 row)

31 days, or 1 month and 3 days: from february 28, one month on is march 28, and three more days reach the 31st. age() is what you want for "how old is this customer", subtraction for "how many days did shipping take". (age() with one argument counts from today, so its answer changes every day.)

weeks start on monday

date_trunc('week', x) rounds down to the monday of x's week, because ISO 8601 weeks start on monday. extract(week ...) gives the ISO week number, and the ISO year it belongs to can be the year before:

daydowweek_startiso_weekiso_yearyear
2026-12-28Mon2026-12-285320262026
2026-12-29Tue2026-12-285320262026
2026-12-30Wed2026-12-285320262026
2026-12-31Thu2026-12-285320262026
2027-01-01Fri2026-12-285320262027
2027-01-02Sat2026-12-285320262027
2027-01-03Sun2026-12-285320262027
2027-01-04Mon2027-01-04120272027
2027-01-05Tue2027-01-04120272027
(9 rows)
real output, postgres 18.6 (PGlite 0.5.12) · dates-and-intervals
nine days around new year 2027, one shade per week. january 1 2027 is a friday, so it belongs to the week that started monday december 28: ISO week 53 of 2026. year and iso_year disagree for three days.

so a weekly report keyed on extract(year ...) and extract(week ...) puts january 1 2027 in "week 53 of 2027", a week that doesn't exist. use isoyear with week, or just group on date_trunc('week', ...), which gives the monday itself:

sqldb=events
SET TimeZone = 'UTC';
SELECT date_trunc('week', occurred_at)::date AS week, count(*) AS events
FROM events
WHERE occurred_at < '2026-03-30'
GROUP BY 1
ORDER BY 1;
output, from postgres 18.6
#1 · SET
#2 · SELECT 5
weekdateeventsint8
2026-02-23275
2026-03-021922
2026-03-091924
2026-03-161923
2026-03-231923
(5 rows)

occurred_at is a timestamptz, so where a week starts depends on the session's time zone, which is why that block sets it. timestamptz is about that. a week starting on sunday is date_trunc('week', x + interval '1 day') - interval '1 day'.

keylines

  • date ± integer moves by days; date − date is a number of days.
  • an interval is months, days and microseconds, kept apart.
    • 1 month clamps to the month's end, so recurring dates should be computed from the start.
  • age() counts calendar months and days; subtraction counts days.
  • date_trunc('week') gives the monday. group by it, or use isoyear with week.