How to parse a String into Datetime in Python


PHOTO EMBED

Wed Jan 22 2020 18:52:28 GMT+0000 (UTC)

Saved by @logicloss01 #python #dates #functions #python3.8

from datetime import datetime

datetime_object = datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')
content_copyCOPY

Resulting object is in native time zone of user. 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.

https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime