PyDocs

Nav

Data Types

datetime.strptime

datetime

classmethod of datetime
  • datetime.strptime(date_string, format)
Parse string according to the given date and time format (like time.strptime()).

For a list of supported format codes, see the documentation:
    https://docs.python.org/3/library/datetime.html#format-codes
Changed in 3.15
  • 3.15: If format specifies a day of month (%d) without a year, ValueError is raised. This is to avoid a quadrennial leap year bug in code seeking to parse only a month and day as the default year used in absence of one in the format is not a leap year. The workaround is to always include a year in your format. If parsing date_string values that do not have a year, explicitly add a year that is a leap year before parsing: >>> import datetime as dt >>> date_string = "02/29" >>> when = dt.datetime.strptime(f"{date_string};1984", "%m/%d;%Y") # Avoids leap year bug. >>> when.strftime("%B %d") 'February 29'
1 Example
>>> import datetime as dt
>>> date_string = "02/29"
>>> when = dt.datetime.strptime(f"{date_string};1984", "%m/%d;%Y")  # Avoids leap year bug.
>>> when.strftime("%B %d")
'February 29'
from the Python docs
See Also
No see-alsos for datetime/datetime.strptime
0 Notes
No notes for datetime.strptime