2

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'

1

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot!! Really appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.