Format timedelta to string

后端 未结 28 1767
春和景丽
春和景丽 2020-11-22 03:57

I\'m having trouble formatting a datetime.timedelta object.

Here\'s what I\'m trying to do: I have a list of objects and one of the members of the cl

28条回答
  •  礼貌的吻别
    2020-11-22 04:02

    Thanks everyone for your help. I took many of your ideas and put them together, let me know what you think.

    I added two methods to the class like this:

    def hours(self):
        retval = ""
        if self.totalTime:
            hoursfloat = self.totalTime.seconds / 3600
            retval = round(hoursfloat)
        return retval
    
    def minutes(self):
        retval = ""
        if self.totalTime:
            minutesfloat = self.totalTime.seconds / 60
            hoursAsMinutes = self.hours() * 60
            retval = round(minutesfloat - hoursAsMinutes)
        return retval
    

    In my django I used this (sum is the object and it is in a dictionary):

    {{ sum.0 }}    
    {{ sum.1.hours|stringformat:"d" }}:{{ sum.1.minutes|stringformat:"#02.0d" }}
    

提交回复
热议问题