25471

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}}

Answer1:

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 = 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

Recommend

  • Facebook Developer Account Verify
  • Eclipse can't find MinGW. Why?
  • Class to DataSet / DataSet to class
  • Format DateAsOrdinal xAxis labels in ZedGraph
  • EJB stateless - Private members initialisation
  • How to set repeating alarm using setExact and how to cancel the same?
  • Best way to display non-public images in a rails view
  • Allowing my app users to post tweets from a single account
  • Timer once a minute on the minute
  • Convert a 12 hour time format to 24 hour time format (keeping record of the day) in python
  • 1º Day of Daylight Saving Time Java and JS showing a different behavior
  • Two columns in subquery in where clause
  • MySql - get days remaining
  • Packet modification with netfilter queue?
  • PHP file_exists() anomaly
  • Returning this from a constructor function in JS
  • SQL: Getting the physical size of a subset of a table
  • What is the use of a session store?
  • How to get the date of next specified day of week
  • Put value at centre of bins for histogram
  • Android cannot disable cut copy paste
  • Calculate time difference in hh:mm:ss with simple javascript/jquery
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Illegal mix of collations for operation for date/time comparison
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • How to make Safari send if-modified-since header?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Release, debug version and Authorization Google?
  • Web-crawler for facebook in python
  • AT Commands to Send SMS not working in Windows 8.1
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • Converting MP3 duration time
  • How can I use threading to 'tick' a timer to be accessed by other threads?