Search code examples
pythonstringdatetimeiso8601strptime

Converting a string into date time


I have the following date in python, as a string which I want to convert to datetime

I tried using strptime function, but due to the format with decimals and a marginal value, it keeps failing. Any idea on how to convert it? The date string structure is like this example: 2022-02-08 17:12:35.667000+00:00


Solution

  • The dateutil module has something just for this. You can use its parser to create a datetime object from a string like this.

    from dateutil import parser
    
    date_obj = parser.parse("2022-02-08 17:12:35.667000+00:00")
    

    EDIT:

    I don't believe that this is a default python library, so you can type this code into the terminal to install it with pip: pip install python-dateutil