Python 如何在django模板中将秒转换为hh:mm:ss
所以我有一本字典叫match。这有比赛的细节,其中一个关键是比赛时间。如果游戏时间使用此格式Python 如何在django模板中将秒转换为hh:mm:ss,python,html,django,templates,Python,Html,Django,Templates,所以我有一本字典叫match。这有比赛的细节,其中一个关键是比赛时间。如果游戏时间使用此格式 {{your_date_field|time:"h:i a"}} 尝试将秒/毫秒转换为实际时间。由于某些原因,这对我来说不起作用。比赛时间任何时间或数字都只是一个数字。比如355。对我来说,这意味着355秒,所以我想将其表示为hh:mm:ss。看到了吗?它将帮助您在视图中将秒转换为时间。在模板中没有方法这样做吗? Could not parse the remainder: ': "Z"' from
{{your_date_field|time:"h:i a"}}
尝试将秒/毫秒转换为实际时间。由于某些原因,这对我来说不起作用。比赛时间任何时间或数字都只是一个数字。比如355。对我来说,这意味着355秒,所以我想将其表示为hh:mm:ss。看到了吗?它将帮助您在视图中将秒转换为时间。在模板中没有方法这样做吗?
Could not parse the remainder: ': "Z"' from 'match.game_time|date: "Z"'
{{your_date_field|time:"h:i a"}}
from datetime import datetime, timedelta
def millisec_to_time(millisec):
d = datetime(1, 1, 1)+millisec
if d.day - 1 == 0:
return "{0}:{1}:{2}".format(d.hour, d.minute, d.second)
else:
return "{0}:{1}:{2}:{3}".format(d.day-1, d.hour, d.minute, d.second)