register filter def board_time time delta datetime now time today date

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
@register.filter
def board_time(time):
delta = datetime.now() - time
today = datetime.now().replace(hour=0, minute=0, second=0)
yesterday = today - timedelta(days=1)
if delta.days == 0 and delta.seconds < 3600:
minutes = int(delta.seconds / 60)
return '%d minutes ago' % minutes
else:
if time > today:
return 'today, %s' % time.strftime('%H:%M')
elif time > yesterday:
return 'yesterday, %s' % time.strftime('%H:%M')
else:
return time.strftime('%d %b, %Y %H:%M')