matousc
matousc

Reputation: 3977

Python datetime.strptime - parse complicated string

I want to make datetime from following string '2015-06-29T11:55:30.000000Z'.

I tried:

import datetime
print datetime.datetime.strptime("2015-06-29T11:55:30.000000Z", "%y-%m-%dT%H:%M:%S.000000Z")   

and I obtained following error:

Traceback (most recent call last):
  File "x1.py", line 5, in <module>
    print datetime.datetime.strptime(date, "%y-%m-%dT%H:%M:%S.000000Z")   
  File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data '2015-06-29T11:55:30.000000Z' does not match format '%y-%m-%dT%H:%M:%S.000000Z'

What I am doing wrong? How to parse it?

Upvotes: 2

Views: 247

Answers (1)

unutbu
unutbu

Reputation: 880717

Capitalize %Y:

import datetime as DT
DT.datetime.strptime("2015-06-29T11:55:30.000000Z", "%Y-%m-%dT%H:%M:%S.000000Z")   
# datetime.datetime(2015, 6, 29, 11, 55, 30)

%y matches 2-digit years -- years without century as a decimal number year. %Y matches 4-digit years.

Upvotes: 2

Related Questions