# SQLite julianday(): days between dates

Source: https://sqlstreak.com/sql/functions/julianday · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**Short answer:** `julianday(date)` returns the date as a Julian day number, a decimal count of days, so `julianday(d2) - julianday(d1)` is the number of days between two dates. PostgreSQL doesn't need it: subtracting one date from another already gives days.

## Syntax

- PostgreSQL: `d2 - d1  -- days between two dates` (No: subtract dates; to_char(d, 'J') gives a whole Julian day number)
- SQLite: `julianday(time_value [, modifier, …])` (Yes, built in)

## Examples

### Days from joining to the first order

PostgreSQL:

```sql
SELECT c.name,
       MIN(o.ordered_on) - c.joined_on AS days
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name, c.joined_on
ORDER BY days
LIMIT 5;
```

SQLite:

```sql
SELECT c.name,
       CAST(julianday(MIN(o.ordered_on)) - julianday(c.joined_on) AS INTEGER) AS days
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name, c.joined_on
ORDER BY days
LIMIT 5;
```

Result (both engines):

| name | days |
| --- | --- |
| Marcus Bell | 135 |
| Lena Fischer | 183 |
| Sam Okafor | 230 |
| Ellie Park | 263 |
| Tomás Herrera | 307 |

### How long each waiting pet has waited (to 1 July 2026)

PostgreSQL:

```sql
SELECT name, arrived_on,
       DATE '2026-07-01' - arrived_on AS days_waiting
FROM pets
WHERE adopted = false
ORDER BY days_waiting DESC
LIMIT 5;
```

SQLite:

```sql
SELECT name, arrived_on,
       CAST(julianday('2026-07-01') - julianday(arrived_on) AS INTEGER) AS days_waiting
FROM pets
WHERE adopted = false
ORDER BY days_waiting DESC
LIMIT 5;
```

Result (both engines):

| name | arrived_on | days_waiting |
| --- | --- | --- |
| Tofu | 2025-04-04 | 453 |
| Gizmo | 2025-05-23 | 404 |
| Bruno | 2025-06-30 | 366 |
| Rocket | 2025-07-09 | 357 |
| Socks | 2025-08-17 | 318 |

### Average days from sign-up to subscribing

PostgreSQL:

```sql
SELECT ROUND(AVG(s.started_on - u.signed_up_on), 1) AS avg_days
FROM subscriptions s
JOIN users u ON u.id = s.user_id;
```

SQLite:

```sql
SELECT ROUND(AVG(julianday(s.started_on) - julianday(u.signed_up_on)), 1) AS avg_days
FROM subscriptions s
JOIN users u ON u.id = s.user_id;
```

Result (both engines):

| avg_days |
| --- |
| 10 |

## Edge cases, measured on both engines

| Case | PostgreSQL | SQLite |
| --- | --- | --- |
| A date | '2461115' | 2461114.5 |
| Days between two dates | 14 | 14 |
| Hours between two timestamps | 8.5 | 8.49999999627471 |
| Across a leap day | 2 | 2 |
| Text that isn't a date | error: invalid input syntax for type date: "soon" | NULL |
| NULL | NULL | NULL |

## Common mistakes

- Subtracting date text in SQLite: `'2026-03-15' - '2026-03-01'` subtracts the leading numbers and gives 0. Use julianday on both sides.
- Forgetting the fraction: julianday('2026-03-15') ends in .5 (Julian days start at noon). Round or CAST the difference.
- Using julianday in PostgreSQL: it doesn't exist; subtract the dates.
- Trusting the decimals: julianday works in floating point, so 8.5 hours came back as 8.49999999627471 (see the matrix). ROUND the result.
