all Python 3timefunction
time.strptime()
formatting
Parses a string according to a format and returns a struct_time; a mismatch with the format raises ValueError.
Syntax
time.strptime(string[, format])
It returns a struct_time, not a datetime: for date arithmetic use datetime.strptime — a class method with the same argument order. Missing fields get defaults: no year gives 1900, no date gives 1 January. Without %Z in the format tm_isdst is -1, meaning unknown. Trailing characters after the match also raise ValueError.
Examples
import time
t = time.strptime('30 Nov 2000', '%d %b %Y')
print(t.tm_year, t.tm_mon, t.tm_yday) # → 2000 11 335
print(time.strptime('12:30', '%H:%M')[:3]) # → (1900, 1, 1)
import time
try:
time.strptime('2024-13-01', '%Y-%m-%d')
except ValueError as e:
print(e) # → time data '2024-13-01' does not match format '%Y-%m-%d'
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy