SQL function
SQLite date(): add days and months, start and end of month
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
date(value, modifier, …) returns a date as 'YYYY-MM-DD' text after applying the modifiers in order: date('2026-03-15', '+7 days') is '2026-03-22'. PostgreSQL does the same with date arithmetic: DATE '2026-03-15' + 7.
Syntax and availability
| PostgreSQL | SQLite | |
|---|---|---|
| Available | No date() function: use date arithmetic and date_trunc | Yes, built in |
| Syntax | DATE '2026-03-15' + 7 / d + INTERVAL '1 month' / date_trunc('month', d)::date | date(time_value [, modifier, …]) -- modifiers: '+N days', '+N months', 'start of month', 'weekday N', … |
Examples
A trial that ends 30 days after sign-up
PostgreSQL
SELECT id, signed_up_on,
signed_up_on + 30 AS trial_ends
FROM users
ORDER BY id
LIMIT 5;SQLite
SELECT id, signed_up_on,
date(signed_up_on, '+30 days') AS trial_ends
FROM users
ORDER BY id
LIMIT 5;Result (PostgreSQL and SQLite, identical): 5 rows
| id | signed_up_on | trial_ends |
|---|---|---|
| 1 | 2026-01-01 | 2026-01-31 |
| 2 | 2026-01-02 | 2026-02-01 |
| 3 | 2026-01-02 | 2026-02-01 |
| 4 | 2026-01-02 | 2026-02-01 |
| 5 | 2026-01-03 | 2026-02-02 |
Try it: PostgreSQL playground · SQLite playground
Group by the first day of the month
PostgreSQL
SELECT date_trunc('month', played_on)::date AS month_start, COUNT(*) AS plays
FROM plays
GROUP BY month_start
ORDER BY month_start;SQLite
SELECT date(played_on, 'start of month') AS month_start, COUNT(*) AS plays
FROM plays
GROUP BY month_start
ORDER BY month_start;Result (PostgreSQL and SQLite, identical): 4 rows
| month_start | plays |
|---|---|
| 2026-01-01 | 274 |
| 2026-02-01 | 635 |
| 2026-03-01 | 1015 |
| 2026-04-01 | 483 |
Try it: PostgreSQL playground · SQLite playground
The last day of each hire month
PostgreSQL
SELECT name, hired_on,
(date_trunc('month', hired_on) + INTERVAL '1 month - 1 day')::date AS month_end
FROM staff
ORDER BY hired_on
LIMIT 5;SQLite
SELECT name, hired_on,
date(hired_on, 'start of month', '+1 month', '-1 day') AS month_end
FROM staff
ORDER BY hired_on
LIMIT 5;Result (PostgreSQL and SQLite, identical): 5 rows
| name | hired_on | month_end |
|---|---|---|
| Rosa Vega | 2024-01-10 | 2024-01-31 |
| Omar Haddad | 2024-03-01 | 2024-03-31 |
| Ben Carter | 2024-04-20 | 2024-04-30 |
| Lia Moreau | 2024-06-15 | 2024-06-30 |
| Ivan Petrov | 2024-08-01 | 2024-08-31 |
Try it: PostgreSQL playground · SQLite playground
How it behaves: edge cases, measured
Each expression was run as SELECT … on PostgreSQL 18.6 and SQLite 3.54.1 when this page was built. Nothing here is typed by hand.
| Case | PostgreSQL | SQLite |
|---|---|---|
| Plus 7 days | DATE '2026-03-15' + 7'2026-03-22' | date('2026-03-15', '+7 days')'2026-03-22' |
| Plus 1 month from 31 January | (DATE '2026-01-31' + INTERVAL '1 month')::date'2026-02-28' | date('2026-01-31', '+1 month')'2026-03-03' |
| Start of month | date_trunc('month', DATE '2026-03-15')::date'2026-03-01' | date('2026-03-15', 'start of month')'2026-03-01' |
| Last day of February 2024 | (date_trunc('month', DATE '2024-02-10') + INTERVAL '1 month - 1 day')::date'2024-02-29' | date('2024-02-10', 'start of month', '+1 month', '-1 day')'2024-02-29' |
| Next Monday on or after | not available | date('2026-03-15', 'weekday 1')'2026-03-16' |
| 30 February | DATE '2026-02-30'error: date/time field value out of range: "2026-02-30" | date('2026-02-30')'2026-03-02' |
| Text that isn't a date | DATE 'March 1'error: invalid input syntax for type date: "March 1" | date('March 1')NULL |
| A timestamp loses its time | TIMESTAMP '2026-03-15 23:59:59'::date'2026-03-15' | date('2026-03-15 23:59:59')'2026-03-15' |
| SQLite's modifiers in PostgreSQL | date('2026-03-15', '+7 days')error: function date(unknown, unknown) does not exist | not available |
Related
Common mistakes
Adding a month to the 29th, 30th or 31st: SQLite and PostgreSQL disagree (see the matrix). Go to the start of the month first if that matters.
Putting modifiers in the wrong order: they apply left to right.
Using SQLite's modifiers in PostgreSQL:
date(d, '+7 days')fails there (see the matrix). Add an integer or an INTERVAL instead.
PostgreSQL and SQLite
SQLite's date() with modifiers has no direct PostgreSQL twin; PostgreSQL adds integers and intervals to dates and truncates with date_trunc. The matrix shows where results differ: month overflow, invalid days (SQLite returns NULL or rolls over, PostgreSQL raises an error).
Official documentation
Go deeper
- Guide: SQL date functions in PostgreSQL and SQLite
- Practice: SQL date practice: 7 exercises in PostgreSQL and SQLite
- Lesson: Date maths (Pro)
Make it stick in five minutes a day
“Date maths” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.