4337 links
  • Arnaud's links
  • Home
  • Login
  • RSS Feed
  • ATOM Feed
  • Tag cloud
  • Picture wall
  • Daily
Links per page: 20 50 100
page 1 / 1
1 results tagged filter x
  • thumbnail
    python - Displaying a timedelta object in a django template - Stack Overflow

    Here's my filter code

    from django import template

    register = template.Library()

    @register.filter()
    def smooth_timedelta(timedeltaobj):
    """Convert a datetime.timedelta object into Days, Hours, Minutes, Seconds."""
    secs = timedeltaobj.total_seconds()
    timetot = ""
    if secs > 86400: # 60sec 60min 24hrs
    days = secs // 86400
    timetot += "{} days".format(int(days))
    secs = secs - days*86400

    if secs > 3600:
        hrs = secs // 3600
        timetot += " {} hours".format(int(hrs))
        secs = secs - hrs*3600
    
    if secs > 60:
        mins = secs // 60
        timetot += " {} minutes".format(int(mins))
        secs = secs - mins*60
    
    if secs > 0:
        timetot += " {} seconds".format(int(secs))
    return timetot

    Then in my template I did

    {% load smooth_timedelta %}

    {% timedeltaobject|smooth_timedelta %}

    October 29, 2020 at 10:25:50 AM GMT+1 - permalink - archive.org - https://stackoverflow.com/questions/16348003/displaying-a-timedelta-object-in-a-django-template
    django filter python
Links per page: 20 50 100
page 1 / 1
Shaarli - The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community - Help/documentation