Possible Duplicate:
Converting unix timestamp string to readable date in Python
When I do time.time()
I get something like: 1342648659.223746
. How can I convert it to unix time that looks like this: 2012-07-18T21:42:00Z
?
Possible Duplicate:
Converting unix timestamp string to readable date in Python
When I do time.time()
I get something like: 1342648659.223746
. How can I convert it to unix time that looks like this: 2012-07-18T21:42:00Z
?
Python's standard datetime module is quite handy for all kinds of date/time manipulation.
For your particular problem, the isoformat()
method is handy. For example, if you already have a time in seconds since the Epoch (such as your 1342648659.223746
):
>> from datetime import datetime
>> datetime.fromtimestamp(1342648659.223746).isoformat()
== '2012-07-18T23:57:39.223746'
And if you just want the current (UTC) time, ISO formatted:
>> datetime.utcnow().isoformat()
== '2012-07-19T00:06:45.724468'
If you absolutely require the timezone information in your output (i.e. the Z
for UTC in your example), you'll either have to append it manually, or fall back to using strftime
:
>> datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
== '2012-07-18T22:11:05Z'
You should use the datetime
module as below:
>>> import datetime
>>> d = datetime.datetime.fromtimestamp(27654727.23)
>>> print d
1970-11-16 22:52:07.230000
You could pass the timestamp to the fromtimestamp()
function.