0

I have a template tag

 @register.filter
def get_total(date_start=None, date_end=None):
    fmt = '%Y-%m-%d %H:%M:%S%f'
    if date_start is not None and date_end is not None:
        ds = str(date_start)
        new_ds = ds[:19]
        de = str(date_end)
        new_de = de[:19]
        date_start = datetime.strptime(new_ds, fmt)
        date_end = datetime.strptime(new_de, fmt)
        return date_end - date_start
    else:
        return None

and am passing it into my website as like this,

<td>{{ table.start_time|get_total:table.Date_Time_End }}</td>

One of my result look like this, "16 days, 13:39:59.900000" I dont want to see the extra 900000. Any way?

BLACK PANTHER
  • 145
  • 1
  • 4
  • 15

1 Answers1

0

Your template tag returns a timedelta, and timedelta cannot be formatted with strftime. I found this function in another SO answer:

def strfdelta(tdelta, fmt):
    d = {"days": tdelta.days}
    d["hours"], rem = divmod(tdelta.seconds, 3600)
    d["minutes"], d["seconds"] = divmod(rem, 60)
    return fmt.format(**d)

With this function you can modify your template tag to return a formatted string:

return strfdelta(date_end - date_start, "%D days %H:%M:%S")
Community
  • 1
  • 1
fasouto
  • 4,386
  • 3
  • 30
  • 66