Dates inside the program are objects; outside (on screen, in files, in forms) they are text. Converting between both is a daily task, and it is done with strftime and strptime.
From date to text: strftime
strftime (*string from time*) builds a text according to a pattern of codes that start with %.
| Code | Means | Example |
|---|---|---|
%Y | 4-digit year | 2026 |
%y | 2-digit year | 26 |
%m | 2-digit month | 03 |
%d | 2-digit day | 05 |
%H | hour (00-23) | 09 |
%M | minutes | 30 |
%S | seconds | 07 |
%j | day of the year (001-366) | 064 |
%A %B | name of the day and month | depend on the system language |
from datetime import datetime
m = datetime(2026, 3, 5, 9, 30, 7)
print(m.strftime("%d/%m/%Y")) # 05/03/2026
print(m.strftime("%Y-%m-%d %H:%M")) # 2026-03-05 09:30
print(m.strftime("%H:%M:%S")) # 09:30:07
print(m.strftime("%j")) # 064
Day and month names (%A, %B) come out in the language configured on the computer and so are not reliable in programs that must always give the same result. It is safer to keep your own lists of names, as in Format dates.
from datetime import date
MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
f = date(2026, 3, 6)
print(f"{MONTHS[f.month - 1]} {f.day}, {f.year}") # March 6, 2026
From text to date: strptime
strptime (*string parse time*) does the opposite: it interprets a text according to the pattern and returns a datetime. If the text does not match, it raises ValueError.
from datetime import datetime
m = datetime.strptime("15/03/2026", "%d/%m/%Y")
print(m) # 2026-03-15 00:00:00
print(m.date()) # 2026-03-15
m2 = datetime.strptime("2026-03-15 18:45", "%Y-%m-%d %H:%M")
print(m2.hour) # 18
Invalid inputs
Any text that comes from outside may be wrong. Catch ValueError: the pattern detects both different formats and impossible dates.
from datetime import datetime
def read(text):
try:
return datetime.strptime(text, "%d/%m/%Y").date()
except ValueError:
return None
print(read("29/02/2024")) # 2024-02-29
print(read("29/02/2023")) # None (2023 is not a leap year)
print(read("2026-03-15")) # None (different format)
print(read("31/04/2026")) # None (April has 30 days)
You practise it in Dates with a custom format.
ISO format: the best option for storing
To store dates in files, databases or JSON the ISO 8601 format is recommended: isoformat() and fromisoformat(). It does not depend on the language, it is not ambiguous (is 03/04 3 April or 4 March?) and it sorts well as text.
from datetime import date
f = date(2026, 3, 15)
text = f.isoformat()
print(text) # 2026-03-15
print(date.fromisoformat(text) == f) # True
dates = ["2026-03-15", "2025-12-01", "2026-01-20"]
print(sorted(dates)) # ['2025-12-01', '2026-01-20', '2026-03-15'] (sorted without converting)
Dates in JSON
JSON has no date type. The usual approach is to store the ISO text and convert it when reading.
import json
from datetime import date
order = {"id": 7, "date": date(2026, 3, 15).isoformat()}
text = json.dumps(order)
print(text) # {"id": 7, "date": "2026-03-15"}
loaded = json.loads(text)
print(date.fromisoformat(loaded["date"]).year) # 2026