Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"x Days ago' template filter in Django?

Tags:

python

django

I'm looking for a filter that turns a datetime instance into 'x Days' or 'x years y months' format (as on SO). Suggestions? Am I overlooking something very obvious?

like image 206
Yuvi Avatar asked May 10 '09 09:05

Yuvi


People also ask

Can I use filter () in Django template?

Django Template Engine provides filters which are used to transform the values of variables;es and tag arguments. We have already discussed major Django Template Tags. Tags can't modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one's own need.

What does {% %} mean in Django?

{% extends variable %} uses the value of variable . If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

What does the Django template filter pluralize do?

In Sendwithus you can use the pluralize filter to change the suffix of a word. Like Django's pluralize filter it returns a plural suffix if the first argument is an integer greater than 1 or a sequence with more than 1 item. If a plural suffix isn't specified pluralize defaults to 's'.


1 Answers

Have a look at the timesince template filter. It's builtin.

The following returns a humanized diff between now and comment_date (e.g. '8 hours'):

{{ comment_date|timesince }}

The following returns a humanized diff between question_date and comment_date:

{{ comment_date|timesince:question_date }}
like image 106
Ayman Hourieh Avatar answered Nov 15 '22 18:11

Ayman Hourieh