'List is printing datetime.datetime

I have a result list that contains something like

[(datetime.datetime(2013, 1, 1, 0, 1, 14),), (datetime.datetime(2013, 1, 1, 1, 33, 50),)]

How does one print it like 2013-01-01 00:01:14.000 , 2013-01-01 01:33:50.000 or a readable format.

I have tried printing them in string

import datetime
...
for date in results:
    print (str(date))

This prints

(datetime.datetime(2013, 1, 1, 0, 1, 14),)
(datetime.datetime(2013, 1, 1, 1, 33, 50),)


Solution 1:[1]

[(datetime.datetime(2013, 1, 1, 0, 1, 14),), (datetime.datetime(2013, 1, 1, 1, 33, 50),)] is a list where each element is a 1-element tuple.

You are printing the tuple rather than the datetime within at the first position, print(str(date[0])) to get the actual value.`

Solution 2:[2]

You need to extract the datetime object out of the tuple, for it to be printed in string format.

>>> for each in results:
...     print(each[0])
...
2013-01-01 00:01:14
2013-01-01 01:33:50
>>>

Solution 3:[3]

Just convert the tuple to string like:

str(datetime_object)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Uku Loskit
Solution 2 Sandeep R Venkatesh
Solution 3 Alexey Khachatryan