Remove trailing data from Django timesince

Is there a way to remove the trailing data from the django timesince filter?

I would like to only display days, weeks months or years without any trailing information. e.g weeks + days -> weeks, months + weeks -> months, years + months -> years, etc.

Additionally, if the date is less than one day, it should display the hours. e.g. 1 hour ago, 4 hours ago, etc.

Currently I have a datetime object and am using the filter like this:

{{ my_date_time|timesince}}


You can make your own template tag and use it to modify the output of timesince to be anything you like. Here's an example just to get you started:

def custom_timesince(value): now = # can add some error checking if you want diff = now - value if diff < timedelta(days=1): return "recently" # or w/e you wanted with the hours # remove trailing information from timesince return timesince(value).split(", ")[0]

possibly helpful: docs on using such custom tags

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:Remove trailing data from Django timesince