[Django]-Django – Timeuntil Tag output abbreviation

4πŸ‘

βœ…

Take a look to timesince from django source code:

chunks = (
    (60 * 60 * 24 * 365, ungettext_lazy('%d year', '%d years')),
    (60 * 60 * 24 * 30, ungettext_lazy('%d month', '%d months')),
    (60 * 60 * 24 * 7, ungettext_lazy('%d week', '%d weeks')),
    (60 * 60 * 24, ungettext_lazy('%d day', '%d days')),
    (60 * 60, ungettext_lazy('%d hour', '%d hours')),
    (60, ungettext_lazy('%d minute', '%d minutes'))
)

The fast and easy way to change it is wrote your custom template filter to change hours by Hr:

def my_time_abbr(value): 
    return value.replace( 'hours', 'Hr').replace('minutes','Min')

In your template:

{{ somedata | timeuntil | my_time_abbr }}

You can also rewrite timesince filter from scratch (copy paste from django timesince) if you are working in Internationalization mode.

πŸ‘€dani herrera

Leave a comment