使用Python在Django中将Unix时间戳转换为人类格式 [英] Convert Unix Timestamp to human format in Django with Python

查看:84
本文介绍了使用Python在Django中将Unix时间戳转换为人类格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串(例如1277722499.82)中的unix时间戳转换为更人性化的格式(hh:mm:ss或类似格式)。有没有简单的方法可以在Django应用的python中做到这一点?这是我想要在模型中的模板之外。谢谢。

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.

编辑
我正在使用python函数time.time()生成时间戳。根据文档:

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


time.time()

time.time()

将时间返回为以秒表示的浮点数自时代以来的
(以UTC为单位)。请注意,即使
的时间始终是作为浮点数返回的
,但
并非所有系统都以比1秒更好的精度提供
的时间。虽然
这个函数通常会返回
个非递减值,但如果
的系统时钟已在

之间退回,则它可以返回比前一个调用低
的值。

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.


推荐答案

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

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

这篇关于使用Python在Django中将Unix时间戳转换为人类格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆