详解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


猜你喜欢
- 解析url用的类库:python2版本: from urlparse import urlparseimport urllibpython3
- 问题有一个元素序列,想知道在序列中出现次数最多的元素是什么解决方案collections 模块中的 Counter 类转让给女士为此问题所设
- 目的临床数据的记录时间和对应标签(逗号后面的数字)记录在txt文件里,要把标签转换为3类标签,并且计算出每个标签的分别持续时间,然后绘制成柱
- 1 锁片化的产生1.1 产生碎片化的原因1、在B-tree索引中,表数据按照聚集索引的排序进行物理存储,若聚集索引离散化比较严重,那么可能会
- 我使用的python版本是3.5.2今天想做个语音读取的小脚本,在网上查了一下发现python里有个pyttsx可以识别文字,打算通过pip
- 复制数据库前要先确认目标数据库的服务是否启动主要是SQL Server (MSSQLSERVER)这个服务要启动起来其他的看自己的需求复制数
- 数据库的使用过程中由于程序方面的问题有时候会碰到重复数据,重复数据导致了数据库部分设置不能正确设置……方法一以下为引用的内容:declare
- 本文实例讲述了Python爬虫之pandas基本安装与使用方法。分享给大家供大家参考,具体如下:一、简介:Python Data Analy
- 1.watch * 引入watchimport { ref, reactive, watch, toRefs } from 'vue
- --创建测试数据库 CREATE DATABASE Db GO --对数据库进行备份 BACKUP DATABASE Db TO DISK=
- 本文深入分析了Symfony控制层。分享给大家供大家参考,具体如下:Symfony中控制层包含了连接业务逻辑与表现的代码,控制层为不同的使用
- 一、oracle oracle服务器有Oracle instace 和Oracle database instance有memory str
- 前言之前用过Eel做的桌面应用觉得已经够 * 了,不过由于Eel是调用Chrome,时常出现各种小问题,比如窗口大小设置后有时候不管用,鼠标右键
- 一、介绍数据库的约束是对表中数据进行的一种限制,为了保证数据的正确性、有效性、完整性。无论是在添加数据还是在删除数据的时候,都能提供帮助。所
- 如何更改 pandas dataframe 中两列的位置:把其中的某列移到第一列的位置。原来的 df 是:df = pd.read_csv(
- 继续Mootools常用方法扩展,依然还是String类的扩展。方法:format说明:一个非常简单的format方法,和C#
- 只有pd模型文件, 打印所有节点from tensorflow.python.framework import tensor_utilfro
- 安装模块如果使用的是Linux系统,并且安装了pip,可以直接使用pip安装xlrd, xlwt:pip install xlwtpip i
- 方法一 delete from test; alter table `test` auto_increment=1 (好处,可以设置AUTO
- 本文实例为大家分享了Python实现图片格式转换的具体代码,供大家参考,具体内容如下碰上这样一个情景:我从网络上下载了一张表情包图片,存放在