Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display "x days ago" type time using Humanize in Django template?

When I do this:

{% load humanize %}

{{ video.pub_date|naturaltime|capfirst }}

I get 2 days, 19 hours ago

How can I get just 2 days without the hours. Basically if the video was published in less than a day ago then it should say X hours ago, then it should count in days like X days ago, then in weeks. I just don't want 1 hours 5 minutes ago or 2 days 13 minutes ago. Just the first part.

I looked at the humanize docs but couldn't find what I needed.

like image 736
Veme Avatar asked Jun 27 '11 15:06

Veme


2 Answers

Django has a built-in template filter timesince that offers the same output you mentioned above. The following filter just strips the second part after the comma:

from datetime import datetime, timedelta
from django import template
from django.utils.timesince import timesince

register = template.Library()

@register.filter
def age(value):
    now = datetime.now()
    try:
        difference = now - value
    except:
        return value

    if difference <= timedelta(minutes=1):
        return 'just now'
    return '%(time)s ago' % {'time': timesince(value).split(', ')[0]}
like image 191
Bernhard Vallant Avatar answered Oct 20 '22 07:10

Bernhard Vallant


You should duplicate your django.contrib.humanize.templatetags.humanize.py to myapp.templatetags.myhumanize and modify it to your needs. (I can't find the actual line that returns "x days, y hours ago". Which version of django/humanize are you using?)

like image 44
Udi Avatar answered Oct 20 '22 05:10

Udi