Ad

Convert String In Format Datetime

- 1 answer

I have this date that comes to me in the following format and it is string type

from datetime import datetime 

fecha_str = "2021-09-27T20:42:34.099000Z"

fecha_datetime = datetime.strptime(fecha_str,'%d del %m de %Y a las %H:%M')

I need to transform it into a datetime type varibale so that I can manipulate it and only show the information that is needed.

Ad

Answer

The format string you're passing to strptime() is for an output (ie, used with strftime()). The timestamp you are parsing is in UTC (the trailing 'Z'), and ISO8601 format with milliseconds.

>>> fecha_str = "2021-09-27T20:42:34.099000Z"
>>> fecha_datetime = datetime.strptime(fecha_str, "%Y-%m-%dT%H:%M:%S.%fZ")

>>> fecha_datetime
datetime.datetime(2021, 9, 27, 20, 42, 34, 99000)
Ad
source: stackoverflow.com
Ad