Home > Software design >  converting json timestamp to datetime in python
converting json timestamp to datetime in python

Time:11-22

How can I convert '2021-11-21T18:57:18.000 01:00' from json to datetime in Python?

  timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ")

is giving me error: ValueError: time data '2021-11-21T18:57:18.000 01:00' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'

CodePudding user response:

Your formatting string is slightly wrong. You need %z to parse the zone offset:

from datetime import datetime

timestamp = '2021-11-21T18:57:18.000 01:00'
timestamp = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f%z")

print(timestamp)

Result:

2021-11-21 18:57:18 01:00
  • Related