Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert string in format datetime

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading