详解Django将秒转换为xx天xx时xx分
作者:FatPuffer 发布时间:2023-06-14 22:52:12
标签:Django,转换
Django将秒转换为xx天xx时xx分,具体代码如下所示:
from django.utils.translation import ngettext_lazy as _n
def humanize_seconds(secs):
a_day = 86400
an_hour = 3600
a_minute = 60
timetot = ''
total_secs = secs
if secs > a_day: # 60sec * 60min * 24hrs
days = int(secs // a_day)
# timetot += "{} {}".format(int(days), _('days'))
timetot += _n('%(num)s day', '%(num)s days', days) % {'num': days}
secs = secs - days * a_day
if secs > an_hour:
hrs = int(secs // an_hour)
# timetot += " {} {}".format(int(hrs), _('hours'))
timetot += ' '
timetot += _n('%(num)s hour', '%(num)s hours', hrs) % {'num': hrs}
secs = secs - hrs * an_hour
if secs > a_minute and total_secs < a_day:
mins = int(secs // a_minute)
timetot += ' '
timetot += _n('%(num)s minute', '%(num)s minutes', mins) % {'num': mins}
secs = secs - mins * a_minute
if secs > 0 and total_secs < an_hour:
secs = int(secs)
timetot += ' '
timetot += _n('%(num)s second', '%(num)s seconds', secs) % {'num': secs}
return timetot
if __name__ == "__main__":
print(humanize_seconds(360200))
知识点扩展:django 将model转换为字典
from django.forms.models import model_to_dict
from projects.models import ProjectInformation
site = ProjectInformation.objects.get(id=6)
dict = model_to_dict(site)
dict
{'CRFmethod': '',
'EDCprovider': '',
'acceptancenum': '',
'add_time': datetime.datetime(2017, 4, 20, 8, 4, 42, 751202, tzinfo=<UTC>),
'begindate': None,
'clinicalassis': '',
'clinicalnum': '',
'created_by': '',
'created_date': None,
'enddate': None,
'ethicsreviewdate': None,
'ethicsreviewpers': '',
'ethicsreviewres': '',
'ethicsreviewunit': '',
'id': 6,
'isimport': None,
'leaderunit': None,
'localcases': None,
'medicalequipment': '',
'mequipmenttype': '',
'multicenter': '',
'plannum': '',
'proenname': '爱上地方',
'proname': '打士大夫',
'prostatus': '',
'prosummary': '',
'protype': '打是否',
'regstudy': '是',
'reportdate': None,
'reportnum': '',
'reportversion': '',
'researchdesign': '',
'researchtype': '',
'responsible': '',
'studytype': '器械类',
'telephonenum': None,
'totalcases': None,
'treatmenttype': None,
'unitnum': None}
总结
以上所述是小编给大家介绍的Django将秒转换为xx天xx时xx分,希望对大家有所帮助
来源:https://blog.csdn.net/qq_42517220/article/details/101292578
0
投稿
猜你喜欢
- 经常会遇到这样一个情况:浏览器弹出对话框,提示脚本运行时间过长,询问“停止”还是“继续”。那究竟各个浏览器是如何判断在什么时候才弹出此对话框
- XML被设计用来描述数据,其焦点是数据的内容。HTML被设计用来显示数据,其焦点是数据的外观。  
- 在Intel的早期,Andy Grove遇到一个雇员 - 他建议公司在芯片的基础上开发个人计算机。AndyGrove疑问道“个人计算机能做什
- 虽然今年名义上已经不再管人了,但也不得不掺和进很多人事,这里想简单说说,即使不能帮助这个行业的从业者规划职业道路,也算是把之前摸过的路小结一
- !DOCTYPE--------------------------------------------------------------
- 2009年 6月6日,空间4岁啦!与此同时Qzone月登录用户2亿,同时在线用户也突破了1000万。这是让人欢欣雀跃的数字,在空间同事眼里,
- 不论你做什么样的设计,色彩都是一个不容忽视的问题。色彩以一种“隐蔽”的方式传达的各种信息,这些信息会影响观看者的心理和感受,左右他们的判断和
- 有关 Web 字体的话题正在增多,对 Web 设计师来说,他们并不关注技术细节,不管是 TrueType 的 Hinting 技术
- 移动互联网被称为“第五次科技革命”,而随着iPhone和Android等智能手机的日渐流行和iPad等平板电脑的出现,移动互联网的潜力和趋势
- Web技术的发展速度太快了,如果你不与时俱进,就会被淘汰。因此,为了应对即将到来的HTML5,本文总结了22个HTML5的初级技巧,希望能对
- 我在前几天的博客中翻译了一篇《20个对学习CSS3大有裨益的资源》的文章,其中推荐了Get started with CSS 3,本文为其中
- Software as a service 软件即服务,21世纪开始兴起的一种完全创新的软件应用模式。客户通过互联网向厂商定购所需的应用软件
- 首先,大家先去下载一份dvbbs.php beta1的代码,解压后先抛开php代码,找出你的mysql手册,如果没有手册那么就直接看下面的实
- 在新建数据库或附加数据库后,想添加关系表,结果出现下面的错误: 此数据库没有有效所有者,因此无法安装数据库关系图支持对象。若要继续,请首先使
- 举例如下,一个服务器端的form 代码自动被解释成客户端代码:服务器端代码: &l
- 在我们开始一个项目的设计的时候,脑子里肯定有无数的构想。是做视觉冲击强烈、夺人眼球的绚丽风格?还是复古的拼贴风?又或者目前最in的极简主义设
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 15 - SlidersMooTools 1.2的
- 阅读上一片:微软建议的ASP性能优化28条守则(1)技巧 3:将数据和 HTML 缓存在 Web 服务器的磁盘上有时,数据可能太多,无法都缓
- js关于 byval 与 byref 二者区别: byval 传递数值,实参和形参分处不同的内存单元,互不干扰! byref 传递地址,实参
- nofollow标签是Google2005年推出的,目的是尽量减少垃圾链接对搜索引擎的影响。有用过网页制作工具的人都知道,在这些工具里是找不