1

I'm trying to convert a datetime string from an XML file ('2016-12-22T21:00:00Z') from this format, I'm using the strptime code ("%Y-%m-%dT%H:%M:%S%Z") but it's giving me an error.

ran = ('2016-12-22T21:00:00Z')
convert = ("%Y-%m-%dT%H:%M:%S%Z")
time = datetime.datetime.strptime(ran, convert)
print(time)
ValueError: time data '2016-12-22T21:00:00Z' does not match format '%Y-%m-%dT%H:%M:%S%Z

After reading all the Q/A's on the website I thought that should be the right code but it appears I'm missing something simple. I just want a format I can use to plot on a graph, I know it's probably an easy fix but I can't figure it out. Thanks

1 Answer 1

2

Remove the % before Z in the format. As %Z stands for time zone name/abbreviation in string format such as UTC, EST, CST with a Full List here, and the letter Z by itself does not match any timezone, you need to match it as a letter.

import datetime
ran = ('2016-12-22T21:00:00Z')
convert = ("%Y-%m-%dT%H:%M:%SZ")
time = datetime.datetime.strptime(ran, convert)

time
# datetime.datetime(2016, 12, 22, 21, 0)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I knew it was something simple I was missing.