Convert Unix Timestamp to human format in Django with Python

╄→尐↘猪︶ㄣ 提交于 2019-12-20 23:29:15

问题


I'd like to a convert unix timestamp I have in a string (ex. 1277722499.82) into a more humanized format (hh:mm:ss or similar). Is there an easy way to do this in python for a django app? This is outside of a template, in the model that I would like to do this. Thanks.

edit I'm using the python function time.time() to generate the timestamp. According to the doc:

time.time()

Return the time as a floating point number expressed in seconds since the epoch, in UTC. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.


回答1:


>>> import datetime
>>> datestring = "1277722499.82"

>>> dt = datetime.datetime.fromtimestamp(float(datestring))
>>> print dt
2010-06-28 11:54:59.820000


来源:https://stackoverflow.com/questions/3133486/convert-unix-timestamp-to-human-format-in-django-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!