Interval arithmetic
Overview
Adding a duration moves a timestamp forward or backward. A calendar month is not a fixed number of days. Decide whether the requirement is a calendar interval or an elapsed duration before choosing an operation.
First example
SQLite 3.46.1.
SELECT date('2024-02-28','+1 day'), unixepoch('2025-01-02 00:00:00') - unixepoch('2025-01-01 00:00:00');Observed rows (JSON notation):
[ [ "2024-02-29", 86400 ]]How it works
PostgreSQL supports interval values and date plus integer days. SQLite uses modifiers such as +1 day and +1 month. Month-end arithmetic is ambiguous: SQLite 3.46.1 defaults to ceiling, and the floor modifier resolves to the last day of the target month. PostgreSQL month addition uses the last valid day when necessary. Use elapsed Unix seconds for fixed-duration calculations in the seconds-resolution fixture.
Practical example
SQLite 3.46.1.
SELECT date('2025-01-31','+1 month'), date('2025-01-31','+1 month','floor');Observed rows (JSON notation):
[ [ "2025-03-03", "2025-02-28" ]]Practice and common mistakes
Do not replace a month by 30 days when implementing monthly renewals. Practice: change January 31 to January 30 in both SQLite month-addition expressions and predict each result. A month-end billing policy needs an explicit rule.
PostgreSQL 17 — not executed
SELECT DATE '2025-01-31' + INTERVAL '1 month';Expected result: 2025-02-28 00:00:00.