Dates and timesPostgreSQL only

AGE

Calculates how much time has passed between two moments — in years, months and days.

Syntax

AGE(<later timestamp>, <earlier timestamp>)
AGE(<timestamp>)

Subtracts the second moment from the first and returns an interval in familiar units — years, months, days — rather than just a number of days. With one argument, it counts from today's date. Hence the name: it's most often used to work out a person's age or length of service.

Data for the examples

orders

idcreated_atamount
12026-07-03 10:15:00120.50
22026-07-28 18:40:0080.25
32026-08-09 09:05:00199.99
42026-08-21 14:30:0045.00
52026-09-02 11:00:0064.75

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

How much time has passed between two dates
SELECT AGE(DATE '2026-09-13', DATE '1990-05-20') AS age;
age
36 years 3 mons 24 days

mons means months: PostgreSQL shows intervals with abbreviated units.

Full years only
SELECT EXTRACT(YEAR FROM AGE(DATE '2026-09-13', DATE '1990-05-20')) AS full_years;
full_years
36
From each order to the end of September
SELECT id, created_at,
AGE(TIMESTAMP '2026-09-30', created_at) AS time_left
FROM orders;
idcreated_attime_left
12026-07-03 10:15:002 mons 26 days 13:45:00
22026-07-28 18:40:002 mons 1 day 05:20:00
32026-08-09 09:05:001 mon 20 days 14:55:00
42026-08-21 14:30:001 mon 8 days 09:30:00
52026-09-02 11:00:0027 days 13:00:00

Watch out

It returns an interval, not a number. Compare it with an interval — AGE(…) > INTERVAL '18 years' — and get the number of years with EXTRACT(YEAR FROM AGE(…)).

The later moment comes first, then the earlier one. Mix them up and you'll get a negative interval: -36 years -3 mons -24 days.

Plain date subtraction is something else: DATE '2026-09-13' - DATE '1990-05-20' gives a number of days, 13265, not years and months.

Related functions

Learn more in the course