Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7873349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:29:14+00:00 2026-06-03T02:29:14+00:00

I currently have my Django custom template filter like this: from django import template

  • 0

I currently have my Django custom template filter like this:

from django import template

register = template.Library()

@register.filter
def duration(value):
    hours   = value / 60
    minutes = value % 60

    hours_string   = str(hours)   + (" hours"   if hours   > 1 else " hour"  ) if hours   else ""
    minutes_string = str(minutes) + (" minutes" if minutes > 1 else " minute") if minutes else ""

    return ' '.join("{hours} {minutes}".format(hours=hours_string, minutes=minutes_string).split())

Some possible outputs:

1 hour 25 minutes
2 hours
45 minutes
3 hours 1 minute

The ' '.join(....split()) trick was something I got from this solution. I realize I only need to check for the extra string at either the beginning or the end of the string (for when it’s 0 hours or 0 minutes), but it seems to do the trick and maybe it’s easier to read than regular expression to some people.

I also feel that I am violating DRY since I am repeating almost the exact same if-then block twice. I was thinking of something like…

for i, unit in enumerate([hours, minutes]):
    # Do some code.

return ' '.join("{hours} {minutes}".format(hours=value[0], minutes=value[1]).split())

but I couldn’t figure out how to choose between “hour(s)” or “minute(s)” without another if-then block. Also the two short-hand if-then per line also looks a bit ugly too…

I’ll appreciate any suggestions or tips. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:29:17+00:00Added an answer on June 3, 2026 at 2:29 am
    import datetime
    def pprint(val):
       if val == 1:
          return '1 minute'
       x = str(datetime.timedelta(minutes=val))[:-3].split(':')
       r = ''
       if int(x[0]):
           r += x[0]+' hours' if int(x[0]) > 1 else x[0]+' hour'
       if int(x[1]):
           r += ' %s minutes' % int(x[1]) if int(x[1]) > 1 else ' %s minute' % int(x[0])
       return r.strip()
    

    Sample run:

    >>> pprint(65)
    '1 hour 5 minutes'
    >>> pprint(1)
    '1 minute'
    >>> pprint(60)
    '1 hour'
    >>> pprint(61)
    '1 hour 1 minute'
    

    You can easily expand this to include days as well, but since you specifically mentioned only hours and minutes I adjusted the method.

    The main heavy lifting is done by datetime.timedelta, the rest is just string parsing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that looks like this: from django.db import models from django.contrib.auth.models
I have a custom django admin page, and I would like to make the
I have a pretty simple form: from django import forms class InitialSignupForm(forms.Form): email =
I'm currently learning Django and some of my models have custom methods to get
I currently have a ModelForm set up in Django, but would like to make
I currently have multiple Django sites running from one Apache server through WSGI and
I currently have a django site, and it's kind of slow, so I want
I currently have a workable Django admin on the left mockup below but want
I currently have a DetailView for Django's built-in User . url( r'^users/(?P<pk>\d+)/$', DetailView.as_view( model
We currently have two EC2 servers running apache servers with django wsgi on freebsd.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.