← Cheat Sheets
Python datetime Format Codes
strftime and strptime directives.
# Python datetime Format Codes
strftime and strptime directives.
| Code | Meaning | Example |
|---|---|---|
| `%Y` | Year, 4 digits | 2025 |
| `%y` | Year, 2 digits | 25 |
| `%m` | Month, zero-padded | 03 |
| `%B` / `%b` | Month name / short | March / Mar |
| `%d` | Day of month | 07 |
| `%A` / `%a` | Weekday name / short | Friday / Fri |
| `%j` | Day of year | 066 |
| `%H` | Hour, 24h | 14 |
| `%I` + `%p` | Hour, 12h + AM/PM | 02 PM |
| `%M` | Minute | 05 |
| `%S` | Second | 09 |
| `%f` | Microseconds | 000123 |
| `%z` | UTC offset | +0100 |
```python
from datetime import datetime, timedelta
datetime.now().strftime("%Y-%m-%d %H:%M")
datetime.strptime("2025-03-07", "%Y-%m-%d")
datetime.now() + timedelta(days=7)
```