詳解Django將秒轉(zhuǎn)換為xx天xx時xx分
Django將秒轉(zhuǎn)換為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))
知識點(diǎn)擴(kuò)展:django 將model轉(zhuǎn)換為字典
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}
總結(jié)
以上所述是小編給大家介紹的Django將秒轉(zhuǎn)換為xx天xx時xx分,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Python使用multiprocessing實現(xiàn)多進(jìn)程的詳細(xì)步驟記錄
multiprocessing包是Python中的多進(jìn)程管理包,與threading.Thread類似,它可以利用multiprocessing.Process對象來創(chuàng)建一個進(jìn)程,下面這篇文章主要給大家介紹了關(guān)于Python使用multiprocessing實現(xiàn)多進(jìn)程的詳細(xì)步驟,需要的朋友可以參考下2024-08-08
淺談python為什么不需要三目運(yùn)算符和switch
下面小編就為大家?guī)硪黄獪\談python為什么不需要三目運(yùn)算符和switch。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
Python定時發(fā)送消息的腳本:每天跟你女朋友說晚安
今天小編就為大家分享一篇關(guān)于Python定時發(fā)送消息的腳本:每天跟你女朋友說晚安的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
關(guān)于pygame.surface.blit()方法4個參數(shù)的使用
這篇文章主要介紹了關(guān)于pygame.surface.blit()方法4個參數(shù)的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03

