4

I am getting

time data '2015-02-10T13:00:00Z' does not match format '%Y-%m-%d %H:%M:%S'

I tried:

import datetime
datetime.datetime.strptime('2015-02-10T13:00:00Z', '%Y-%m-%d %H:%M:%S')

and

import time
time.strptime('2015-02-10T13:00:00Z', '%Y-%m-%d %H:%M:%S')

what am I doing wrong?

doniyor
  • 36,596
  • 57
  • 175
  • 260
  • 1
    Your format balks because you are not handling the `T` and `Z` characters in your input; you have a space instead of the `T` and no timezone handling. – Martijn Pieters Feb 09 '15 at 11:27
  • Isn't it clear that the template you give doesn't match the date/time you're passing? – lurker Feb 09 '15 at 11:27

1 Answers1

7

As a quick workaround, you could add T and Z characters into the datetime formatting:

import datetime            #          v  note  v
datetime.datetime.strptime('2015-02-10T13:00:00Z', '%Y-%m-%dT%H:%M:%SZ')
# datetime.datetime(2015, 2, 10, 13, 0)            #        ^  note  ^

But it's better to use something that is able to parse ISO-formatted date & time. For example, dateutil.parser:

import dateutil.parser
dateutil.parser.parse('2015-02-10T13:00:00Z')
# datetime.datetime(2015, 2, 10, 13, 0, tzinfo=tzutc())
Igor Hatarist
  • 5,234
  • 2
  • 32
  • 45