🔵 🔵 🔵


Primary

၊၊||၊|။

datetime.strptime() ⚬|Documentation|1st|20251021235118-00-⌔

datetime — Basic date and time types — Python 3 documentation#datetime.datetime.strptime

method classdatetime.strptime(date_string, format)

Return a datetime corresponding to date_string, parsed according to format.

If format does not contain microseconds or time zone information, this is equivalent to:

datetime(﹡(time.strptime(date_string, format)[0:6]))

ValueError is raised if the date_string and format can’t be parsed by time.strptime() or if it returns a value which isn’t a time tuple. See also strftime() and strptime() behavior and datetime.fromisoformat().

Changed in version 3.13: If format specifies a day of month without a year a DeprecationWarning is now emitted. 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. Such format values may raise an error as of Python 3.15. 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'

Class attributes:

Printed 2026-06-28.

(echo:: @ )

Link to original

Secondary

• • •