详解Django将秒转换为xx天xx时xx分
框架(架构)  /  管理员 发布于 7年前   195
Django将秒转换为xx天xx时xx分,具体代码如下所示:
from django.utils.translation import ngettext_lazy as _ndef 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 timetotif __name__ == "__main__": print(humanize_seconds(360200))
知识点扩展:django 将model转换为字典
from django.forms.models import model_to_dictfrom projects.models import ProjectInformationsite = 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分,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号