If a Python string contains an ISO-formatted date, skip strptime and use datetime.fromisoformat:
Having a tough time remembering Python strptime vs. strftime? – The “p” (strptime) is for parsing, getting a datetime from a string– The “f” (strftime) is for formatting, getting a string from a datetime For help remembering the format codes, turn to https://www.strfti.me/ !
Want to parse a Python string into a datetime? Use strptime, passing (a) the string and (b) the date format specification: Both return datetime.datetime(2026, 4, 1, 0, 0)
Want to format a Python datetime as a string? Use strftime:
Want a Python datetime with a timezone? Use the “zoneinfo” module: Then pass it to the “tzinfo” keyword arg:
Want a Python datetime? Just invoke datetime.datetime: Omitted time units are 0:
Want to compare two Python datetimes? Just subtract, getting a timedelta:
Need the current date and time in Python?
Generate a Python Pandas DatetimeIndex of datetime values, specifying the frequency, by passing freq and a code: This returns a 184-element DatetimeIndex with evenly-spaced datetime values in that period.
Generate a Python Pandas DatetimeIndex of datetime values with pd.date_range: This returns a 10-element series with evenly-spaced datetime values in that period.