views:

255

answers:

4

I have a string representing a unix timestamp (i.e. "1284101485") in Python, and I'd like to convert it to a readable date. time.strftime returns me with a type error. Any help is appreciated!

A: 

Did you look at the datetime package? I believe it has a fromUnixTimestamp method.

extraneon
It doesn't, but datetime.datetime.fromtimestamp(1284101485) produces datetime.datetime(2010, 9, 10, 8, 51, 25).
Anthony Labarre
That's what I meant:)
extraneon
+6  A: 
>>> from datetime import datetime
>>> datetime.fromtimestamp(1172969203.1)
datetime.datetime(2007, 3, 4, 0, 46, 43, 100000)

Taken from http://seehuhn.de/pages/pdate

Daniel
+2  A: 

Use datetime module:

import datetime
print(datetime.datetime.fromtimestamp(int("1284101485")).strftime('%Y-%m-%d %H:%M:%S'))
Michał Niklas
A: 
>>> import time
>>> time.ctime(int("1284101485"))
'Fri Sep 10 16:51:25 2010'
>>> time.strftime("%D %H:%M", time.localtime(int("1284101485")))
'09/10/10 16:51'
gnibbler