from django import template import datetime register = template.Library() def dayssince(value): "Returns number of days between today and value." today = datetime.date.today() diff = today - value if diff.days > 1: return '%s days ago' % diff.days elif diff.days == 1: return 'yesterday' elif diff.days == 0: return 'today' else: # Date is in the future; return formatted date. return value.strftime("%B %d, %Y") register.filter('dayssince', dayssince)