Query
SQLite documents timediff() for a human-readable difference, but recommends subtracting julianday() or unixepoch() when you need a precise number of days or seconds.
SELECT
ROUND(
julianday('2026-08-23 18:00:00') -
julianday('2026-08-20 06:00:00'),
2
) AS days,
(unixepoch('2026-08-23 18:00:00') -
unixepoch('2026-08-20 06:00:00')) / 3600.0 AS hours;Cases that change the result
PostgreSQL: direct subtraction and intervals
In PostgreSQL, subtracting two date values returns a whole number of days. Subtracting two timestamp values returns an interval. If you want a symbolic difference in years, months and days, age() works differently.
SELECT DATE '2026-08-23' - DATE '2026-08-20' AS days;
SELECT
TIMESTAMP '2026-08-23 18:00:00' -
TIMESTAMP '2026-08-20 06:00:00' AS interval;The first query returns 3 days. The second gives 3 days and 12 hours as an interval.
MySQL: DATEDIFF for days, TIMESTAMPDIFF for other units
DATEDIFF(end, start) uses only the date part and returns days. For hours, minutes, months or other units, TIMESTAMPDIFF(unit, start, end) returns a whole number in the unit you specify.
SELECT DATEDIFF('2026-08-23', '2026-08-20') AS days;
SELECT TIMESTAMPDIFF(
HOUR,
'2026-08-20 06:00:00',
'2026-08-23 18:00:00'
) AS hours;With those values, the answers are 3 days and 84 hours.
SQL Server: DATEDIFF counts datepart boundaries
SQL Server defines DATEDIFF(datepart, start, end) as the number of boundaries of the given unit crossed between the two values. That definition matters: two timestamps a fraction of a second apart can return 1 for day if they straddle midnight.
SELECT DATEDIFF(
hour,
'2026-08-20T06:00:00',
'2026-08-23T18:00:00'
) AS hours;For large intervals measured in very small units there is DATEDIFF_BIG. If you need an exact physical duration, decide on the unit first and check how time zones and date/time types come into play.
Decision table
| Engine | Days | Hours / other units | Key detail |
|---|---|---|---|
| PostgreSQL | date - date | timestamp - timestamp → interval | age() is symbolic. |
| MySQL | DATEDIFF(end,start) | TIMESTAMPDIFF(unit,start,end) | DATEDIFF ignores the time. |
| SQL Server | DATEDIFF(datepart,start,end) | Counts unit boundaries. | |
| SQLite | julianday(end)-julianday(start) | unixepoch for seconds | timediff() is readable, but not an exact numeric measure. |
Avoid this confusion
Treating “months” as fixed 30-day blocks
Months vary in length. Decide whether you want whole calendar months, elapsed days or a symbolic representation.
Swapping the start and end
MySQL DATEDIFF uses end - start, while TIMESTAMPDIFF takes the start first and then the end. Document the convention in your query.