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.
- 1SELECT d::date, (d + interval '1 month')::date,
- 2 (d + interval '1 month' + interval '1 month')::date
- 3FROM generate_series(date '2026-01-28', date '2026-01-31', interval '1 day') AS d;
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.
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.
SELECT n, (date '2026-01-31' + n * interval '1 month')::date AS renews_on
FROM generate_series(0, 4) AS n
ORDER BY n;| nint4 | renews_ondate |
|---|---|
| 0 | 2026-01-31 |
| 1 | 2026-02-28 |
| 2 | 2026-03-31 |
| 3 | 2026-04-30 |
| 4 | 2026-05-31 |
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:
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;| next_daydate | days_in_februaryint4 | typeregtype |
|---|---|---|
| 2026-02-01 | 28 | date |
add an interval instead and you leave dates behind:
SELECT date '2026-01-31' + interval '1 month';SELECT date '2026-01-31' + interval '1 month' AS plus_month,
pg_typeof(date '2026-01-31' + interval '1 month') AS type;| plus_monthtimestamp | typeregtype |
|---|---|
| 2026-02-28 00:00:00 | timestamp without time zone |
the same idea gives you a month's last day: truncate to the first of the month, add a month, step back a day.
SELECT (date_trunc('month', date '2026-02-17') + interval '1 month - 1 day')::date AS last_of_feb;| last_of_febdate |
|---|
| 2026-02-28 |
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:
| interval | months | days | time | justified |
|---|---|---|---|---|
| 36:00:00 | 0 | 0 | 36:00:00 | 1 day 12:00:00 |
| 1 day 12:00:00 | 0 | 1 | 12:00:00 | 1 day 12:00:00 |
| 1 mon | 1 | 0 | 00:00:00 | 1 mon |
| 30 days | 0 | 30 | 00:00:00 | 1 mon |
| 1 mon -01:00:00 | 1 | 0 | -01:00:00 | 29 days 23:00:00 |
| 1 year 2 mons 3 days 04:05:06 | 14 | 3 | 04:05:06 | 1 year 2 mons 3 days 04:05:06 |
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:
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;| equalbool | plus_a_monthtimestamp | plus_30_daystimestamp |
|---|---|---|
| t | 2026-03-01 00:00:00 | 2026-03-03 00:00:00 |
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:
SELECT date '2026-03-31' - date '2026-02-28' AS days,
age(date '2026-03-31', date '2026-02-28') AS age;| daysint4 | ageinterval |
|---|---|
| 31 | 1 mon 3 days |
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:
| day | dow | week_start | iso_week | iso_year | year |
|---|---|---|---|---|---|
| 2026-12-28 | Mon | 2026-12-28 | 53 | 2026 | 2026 |
| 2026-12-29 | Tue | 2026-12-28 | 53 | 2026 | 2026 |
| 2026-12-30 | Wed | 2026-12-28 | 53 | 2026 | 2026 |
| 2026-12-31 | Thu | 2026-12-28 | 53 | 2026 | 2026 |
| 2027-01-01 | Fri | 2026-12-28 | 53 | 2026 | 2027 |
| 2027-01-02 | Sat | 2026-12-28 | 53 | 2026 | 2027 |
| 2027-01-03 | Sun | 2026-12-28 | 53 | 2026 | 2027 |
| 2027-01-04 | Mon | 2027-01-04 | 1 | 2027 | 2027 |
| 2027-01-05 | Tue | 2027-01-04 | 1 | 2027 | 2027 |
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:
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;| weekdate | eventsint8 |
|---|---|
| 2026-02-23 | 275 |
| 2026-03-02 | 1922 |
| 2026-03-09 | 1924 |
| 2026-03-16 | 1923 |
| 2026-03-23 | 1923 |
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.