Skip to content Skip to sidebar Skip to footer

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 +

Solution 1:

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:

defcustom_timesince(value):
    now = datetime.datetime.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

Post a Comment for "Remove Trailing Data From Django Timesince"