Search code examples
pythonpython-dateutil

How to convert string date to dateobject and get the year when string date value is in format 2021-01-22T11:36:52.387000+01:00 with datetime?


I tried this:

 timestamp = "2021-01-22T11:36:52.387000+01:00"
 timestampObject = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')

But gave me error:

ValueError: unconverted data remains: .150000+01:00

What is the rest reprisenting and how do I convert the rest? Also what does the 'T' mean?


Solution

  • This worked for me:

    timestampObject = datetime.fromisoformat(
     "2021-01-22T11:36:52.387000+01:00" ).date()
    
     print('timestampObject.year: ', timestampObject.year) 
    

    timestampObject.year: 2021