Reputation: 8995
I am using {{ prospect.date_1 }} - ({{ prospect.date_1|timesince }} ago)
in my template to get time since the date.
The point is, date_1 is a date
not datetime
, so when i apply the filter it tells me like
July 18, 2014 - (11 hours, 39 minutes ago)
July 18, 2014 - (0 days ago)
Upvotes: 1
Views: 3679
Reputation: 2814
taken from naturalday
@register.filter(expects_localtime=True)
def days_since(value, arg=None):
try:
tzinfo = getattr(value, 'tzinfo', None)
value = date(value.year, value.month, value.day)
except AttributeError:
# Passed value wasn't a date object
return value
except ValueError:
# Date arguments out of range
return value
today = datetime.now(tzinfo).date()
delta = value - today
if abs(delta.days) == 1:
day_str = _("day")
else:
day_str = _("days")
if delta.days < 1:
fa_str = _("ago")
else:
fa_str = _("from now")
return "%s %s %s" % (abs(delta.days), day_str, fa_str)
results
>>> days_since(datetime.now())
'0 days ago'
>>> days_since(date(2013, 5, 12))
'432 days ago'
>>> days_since(date(2014, 12, 12))
'147 days from now'
>>> days_since(date(2014, 7, 19))
'1 day from now'
Upvotes: 6
Reputation: 873
@Jack, Have you tried to use in-built python:
Visit: https://docs.python.org/2/library/datetime.html#datetime.datetime.day
Also if this might help:
https://docs.djangoproject.com/en/1.6/ref/contrib/humanize/#naturaltime
from datetime import date
from datetime import datetime
d = date.today()
datetime.combine(d, datetime.min.time())
Upvotes: 0