详解Django将秒转换为xx天xx时xx分
Django将秒转换为xx天xx时xx分,具体代码如下所示:
fromdjango.utils.translationimportngettext_lazyas_n
defhumanize_seconds(secs):
a_day=86400
an_hour=3600
a_minute=60
timetot=''
total_secs=secs
ifsecs>a_day:#60sec*60min*24hrs
days=int(secs//a_day)
#timetot+="{}{}".format(int(days),_('days'))
timetot+=_n('%(num)sday','%(num)sdays',days)%{'num':days}
secs=secs-days*a_day
ifsecs>an_hour:
hrs=int(secs//an_hour)
#timetot+="{}{}".format(int(hrs),_('hours'))
timetot+=''
timetot+=_n('%(num)shour','%(num)shours',hrs)%{'num':hrs}
secs=secs-hrs*an_hour
ifsecs>a_minuteandtotal_secs0andtotal_secs
知识点扩展:django将model转换为字典
fromdjango.forms.modelsimportmodel_to_dict
fromprojects.modelsimportProjectInformation
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=),
'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分,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!