Date functions
Date functions read the current time, extract parts and do arithmetic with intervals.
In this page:
Syntax
SELECT now(), CURRENT_DATE;
SELECT EXTRACT(field FROM date_column) FROM table_name;
SELECT date_column + INTERVAL 'n days' FROM table_name;
Date functions
now() and CURRENT_DATE return the current time and date. EXTRACT(field FROM date) returns parts such as year, month, dow and epoch, date_trunc(month, ts) rounds down to a unit, age(a, b) gives an interval, and to_char formats dates as text.
Adding INTERVAL values shifts dates.
Note:
date_trunc is the easiest way to group by month.
Example: Date functions
shop=# SELECT EXTRACT(year FROM DATE '2024-03-15') AS y, EXTRACT(dow FROM DATE '2024-03-15') AS weekday;
y | weekday
------+---------
2024 | 5
shop=# SELECT date_trunc('month', TIMESTAMP '2024-03-15 10:30:00') AS month_start;
month_start
---------------------
2024-03-01 00:00:00
shop=# SELECT age(DATE '2024-03-15', DATE '2000-01-01') AS age;
age
-------------------------
24 years 2 mons 14 days
shop=# SELECT DATE '2024-03-15' + INTERVAL '1 month' AS next_month;
next_month
---------------------
2024-04-15 00:00:00
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Formatting dates as text for comparison
- Ignoring time zones with now()
- Using string concatenation for date arithmetic
Chapter Summary
- now() and CURRENT_DATE
- EXTRACT gets parts
- date_trunc rounds down
- INTERVAL adds durations
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: