详解Django将秒转换为xx天xx时xx分

作者:FatPuffer 时间:2023-06-14 22:52:12 

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

标签:Django,转换
0
投稿

猜你喜欢

  • Python使用Pickle库实现读写序列操作示例

    2022-05-15 03:53:03
  • navicat无法远程连接mysql的解决方法

    2024-01-21 13:13:51
  • 网页设计详细教程之XML简便省力技巧五则

    2008-05-23 14:37:00
  • 详解四种Python中基本形态学滤波的实现

    2023-05-09 15:10:09
  • python实现可视化动态CPU性能监控

    2023-08-08 10:23:52
  • tensorflow 实现数据类型转换

    2023-09-20 11:35:47
  • python编写猜数字小游戏

    2021-01-14 11:18:40
  • 详解python使用Nginx和uWSGI来运行Python应用

    2023-07-25 20:40:55
  • python 获取页面表格数据存放到csv中的方法

    2021-01-28 02:13:48
  • MSSQL数据加密解密代码

    2024-01-18 05:22:34
  • Python调用系统底层API播放wav文件的方法

    2021-10-02 06:54:13
  • JavaScript在ASP页面中实现掩码文本框效果代码

    2013-06-01 19:57:23
  • python编写微信远程控制电脑的程序

    2023-08-15 17:04:38
  • PHP容易被忽略而出错陷阱 数字与字符串比较

    2024-05-11 10:10:08
  • python使用xlrd模块读写Excel文件的方法

    2022-02-14 16:54:55
  • Python中itertools库的四个函数介绍

    2021-05-04 14:34:05
  • C#中实现查找mysql的安装路径

    2024-01-24 05:48:15
  • SQL文本字段的数字排序问题

    2008-11-18 16:47:00
  • 如何用Python提取10000份log中的产品信息

    2023-06-30 06:23:21
  • sql server关键字详解大全(图文)

    2024-01-14 09:43:13
  • asp之家 网络编程 m.aspxhome.com