詳解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))
知識點擴展: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分,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Python數(shù)組拼接np.concatenate實現(xiàn)過程
這篇文章主要介紹了Python數(shù)組拼接np.concatenate實現(xiàn)過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04
python按比例隨機切分數(shù)據(jù)的實現(xiàn)
這篇文章主要介紹了python按比例隨機切分數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
Window10+Python3.5安裝opencv的教程推薦
下面小編就為大家分享一篇Window10+Python3.5安裝opencv的教程推薦,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
關(guān)于python3?opencv?圖像二值化的問題(cv2.adaptiveThreshold函數(shù))
這篇文章主要介紹了python3?opencv?圖像二值化cv2.adaptiveThreshold函數(shù)的相關(guān)知識,結(jié)合示例代碼介紹了adaptiveThreshold方法的用法,需要的朋友可以參考下2022-04-04
python實現(xiàn)機械分詞之逆向最大匹配算法代碼示例
這篇文章主要介紹了python實現(xiàn)機械分詞之逆向最大匹配算法代碼示例,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
推薦10款最受Python開發(fā)者歡迎的Python IDE
這篇文章收集了一些對開發(fā)者非常有幫助的,最好的10款Python IDE,包括Vim,Eclipse with PyDev,Sublime Text,PyCharm等知明Python開發(fā)工具2018-09-09
Python容器使用的5個技巧和2個誤區(qū)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于Python容器使用的5個技巧和2個誤區(qū)的相關(guān)知識點內(nèi)容,需要的朋友們學習下。2019-09-09
Tensorflow2.1實現(xiàn)Fashion圖像分類示例詳解
這篇文章主要為大家介紹了Tensorflow2.1實現(xiàn)Fashion圖像分類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

