strftime()
strftime turns a date into text using format codes such as %Y-%m-%d; strptime does the reverse, parsing text into a datetime.
In more detail
The f in strftime is for format and the p in strptime for parse. The format codes must match the text exactly for strptime, or it raises ValueError.
Example
d.strftime("%d %B %Y")
datetime.strptime("2026-09-29", "%Y-%m-%d")Related terms
- datetime module — The standard library module for dates and times as real values you can compare, add to and format, not as text.
- ISO 8601 date format — The international standard for writing dates and times as text, such as 2024-01-15T10:00:00Z.
Taught in
- CGI: Dynamic Web Pages — Hands-on Python: Complete Video Course & Book
- Dates, Times And Random — Hands-on Python: Complete Video Course & Book