如何在django中强制持续时间显示为H:i?

时间:2017-12-15 07:27:05

标签: python django

我有一个持续时间字段,经过验证,最长为24小时 但是,当显示24小时时,它显示为:Glide.with(this).load(uri).into(imageView).getSize(new SizeReadyCallback() { @Override public void onSizeReady(int width, int height) { //before you load image LOG height and width that u actually got? mEditDeskLayout.setImageSize(width,height); } });

我希望它显示为1 day, 0:00:00

我目前正在显示24:00,如下所示:

DurationField

1 个答案:

答案 0 :(得分:0)

您可以为持续时间字段创建自定义窗口小部件来操作输出,如以下答案所示:DurationField format in a ModelForm

from django.forms.widgets import TextInput
from django.utils.dateparse import parse_duration

class DurationInput(TextInput):

    def _format_value(self, value):
        duration = parse_duration(value)
        days = duration.days
        seconds = duration.seconds

        minutes = seconds // 60
        seconds = seconds % 60

        hours = minutes // 60
        minutes = minutes % 60         

        return '{:02d}{:02d}:{:02d}'.format(
            hours if hours != 0 else 24, minutes, seconds
        )

然后在表单中使用它。