# SQLite strftime(): format dates and times

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


**Short answer:** `strftime(format, date, modifiers…)` turns a date or time into text in the format you give: `strftime('%Y-%m', '2026-03-15')` returns `'2026-03'`. It's SQLite's date formatter; PostgreSQL has no strftime and uses `to_char` and `EXTRACT` instead.

## Syntax

- PostgreSQL: `to_char(date_or_timestamp, 'YYYY-MM')  -- the PostgreSQL equivalent` (No: use to_char() or EXTRACT())
- SQLite: `strftime(format, time_value [, modifier, …])` (Yes, built in)

## Examples

### Year and month of each arrival

PostgreSQL:

```sql
SELECT name, arrived_on,
       to_char(arrived_on, 'YYYY') AS year,
       to_char(arrived_on, 'MM') AS month
FROM pets
ORDER BY arrived_on
LIMIT 5;
```

SQLite:

```sql
SELECT name, arrived_on,
       strftime('%Y', arrived_on) AS year,
       strftime('%m', arrived_on) AS month
FROM pets
ORDER BY arrived_on
LIMIT 5;
```

Result (both engines):

| name | arrived_on | year | month |
| --- | --- | --- | --- |
| Tofu | 2025-04-04 | 2025 | 04 |
| Gizmo | 2025-05-23 | 2025 | 05 |
| Bruno | 2025-06-30 | 2025 | 06 |
| Rocket | 2025-07-09 | 2025 | 07 |
| Socks | 2025-08-17 | 2025 | 08 |

### Orders per weekday (0 = Sunday)

PostgreSQL:

```sql
SELECT EXTRACT(DOW FROM ordered_on)::text AS weekday, COUNT(*) AS orders
FROM orders
GROUP BY weekday
ORDER BY weekday;
```

SQLite:

```sql
SELECT strftime('%w', ordered_on) AS weekday, COUNT(*) AS orders
FROM orders
GROUP BY weekday
ORDER BY weekday;
```

Result (both engines):

| weekday | orders |
| --- | --- |
| 0 | 6 |
| 2 | 2 |
| 3 | 3 |
| 4 | 3 |
| 5 | 1 |
| 6 | 2 |

### Sign-ups per month

PostgreSQL:

```sql
SELECT to_char(signed_up_on, 'YYYY-MM') AS month, COUNT(*) AS signups
FROM users
GROUP BY month
ORDER BY month;
```

SQLite:

```sql
SELECT strftime('%Y-%m', signed_up_on) AS month, COUNT(*) AS signups
FROM users
GROUP BY month
ORDER BY month;
```

Result (both engines):

| month | signups |
| --- | --- |
| 2026-01 | 80 |
| 2026-02 | 100 |
| 2026-03 | 120 |

## Edge cases, measured on both engines

| Case | PostgreSQL | SQLite |
| --- | --- | --- |
| Year | '2026' | '2026' |
| Month, zero-padded | '03' | '03' |
| Day of week (0 = Sunday) | 0 | '0' |
| Day of the year | '074' | '074' |
| Week number (they count weeks differently) | '01' | '00' |
| Hours and minutes of a timestamp | '14:05' | '14:05' |
| Seconds since 1970 | 1773532800 | '1773532800' |
| An impossible date | error: date/time field value out of range: "2026-13-01" | NULL |
| NULL | NULL | NULL |

## Common mistakes

- Using strftime in PostgreSQL: it doesn't exist there (see “function does not exist”).
- Comparing strftime's text with a number: `strftime('%Y', d) = 2026` compares text with an integer and is false. Compare with `'2026'`, or CAST the result.
- Expecting `%W` to be the ISO week: it counts weeks starting on Monday from the first Monday of the year; PostgreSQL's `IW` is the ISO week, so the two disagree around New Year (see the matrix).
