3

I have url that returns date in this format

url_date = "2015-01-12T08:43:02Z"

I don't know why there are strings, it would have been simpler to get it as "2015-01-1208:43:02" which would have been simpler to parse using

datetime.datetime.strptime(url_date , '%Y-%m-%d')

but it does not work. I have tried with

%Y-%m-%d
%Y-%m-%d-%H-%M-%S
%Y-%m-%d-%H-%M-%S-%Z

But I keep getting errors like "time data 2015-01-12T08:43:02Z does not match ..."

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
arbi-g11324115
  • 193
  • 1
  • 2
  • 13
  • related: [How to parse an ISO 8601-formatted date in Python?](http://stackoverflow.com/q/127803/4279) – jfs Oct 29 '15 at 13:55

2 Answers2

5

You were getting close with the "Z" in your final attempt - you need to specify the T, Z, and colon literal values in your format string.

>>> import datetime
>>> url_date = "2015-01-12T08:43:02Z"
>>> datetime.datetime.strptime(url_date , '%Y-%m-%dT%H:%M:%SZ')
datetime.datetime(2015, 1, 12, 8, 43, 2)
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • Thanks, just one quick question .. if I wanted to get a time back with the strings as in `2015-01-12:08:43:02` which methods should after `datetime().datetime()` ? – arbi-g11324115 Oct 28 '15 at 17:07
  • in a nutshell, I would like to submit the time to database, and the `DATETIME` field can not accept more than specific number of digits, namely `2015-10-4:06:13:22` so, I would just like to know how it get that back – arbi-g11324115 Oct 28 '15 at 17:12
  • So you want to convert it _from_ a datetime, _to_ a string? Use `datetime_obj.strftime("%Y-%m%d:%H:%M:%S")`. – Kevin Oct 28 '15 at 17:15
5

The format you are looking for is - '%Y-%m-%dT%H:%M:%SZ' .

Example -

>>> url_date = "2015-01-12T08:43:02Z"
>>> import datetime
>>> datetime.datetime.strptime(url_date , '%Y-%m-%dT%H:%M:%SZ')
datetime.datetime(2015, 1, 12, 8, 43, 2)

For the new requirement in comments -

if I wanted to get a time back with the strings as in 2015-01-12:08:43:02 which methods should after datetime().datetime()

You would need to use .strftime() on the datetime.datetime object with the format - '%Y-%m-%d:%H:%M:%S'. Example -

>>> url_date = "2015-01-12T08:43:02Z"
>>> dt = datetime.datetime.strptime(url_date , '%Y-%m-%dT%H:%M:%SZ')
>>> dt.strftime('%Y-%m-%d:%H:%M:%S')
'2015-01-12:08:43:02'

If you wanted the time component , you can use .time() for that. Example -

>>> dt = datetime.datetime.strptime(url_date , '%Y-%m-%dT%H:%M:%SZ')
>>> dt.time()
datetime.time(8, 43, 2)
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • Thanks, just one quick question .. if I wanted to get a time back with the strings as in 2015-01-12:08:43:02 which methods should after datetime().datetime() – arbi-g11324115 Oct 28 '15 at 17:09
  • You want the result as a string? And in that format? If so, check the `.strftime()` example given above. – Anand S Kumar Oct 28 '15 at 17:13
  • I just want it as as string, like your first example `2015-01-12:08:43:02` which I just tried btw, and I get `error, required argument 'format' (pos 1) not found` – arbi-g11324115 Oct 28 '15 at 17:14