objectif
Parser ISO 8601 natif et formater avec strftime.
code minimal
from datetime import datetime
d = datetime.fromisoformat("2025-08-17T10:20:30")
print((d.year, d.month, d.day) == (2025, 8, 17)) # attendu: True
utilisation
from datetime import datetime
s = datetime(2025, 1, 2, 3, 4, 5).strftime("%Y-%m-%d %H:%M:%S")
print(s.startswith("2025-01-02"))
variante(s) utile(s)
from datetime import datetime
d = datetime.strptime("17/08/2025", "%d/%m/%Y")
print(d.month == 8)
notes
- fromisoformat supporte aussi les offsets: “2025-01-01T00:00:00+00:00”.
- Attention aux naïfs vs “aware” (avec tzinfo).