humanize time difference (how long ago)
If you ever have a need to display something like: "last update 5 days ago" "user logged in 2 mins ago" you can use this script to determine how long ago a timestamp is versus now().
- time
- now
- humanize
If you ever have a need to display something like: "last update 5 days ago" "user logged in 2 mins ago" you can use this script to determine how long ago a timestamp is versus now().
Useful for transferring datetime values to JS: briefer and overcoming the 1 vs. 0 based month enumeration difference between python and JS.
Use this to display a split of page execution time between python and the db in your base template when debugging. I originally got the base of this code from another snippet, but I can't find it anymore and want to share with new folks because I find this handy.
l = (1, 23, 60, 75, 3600, 36000) for n in l: print friendly_seconds(n) 00:00:01 00:00:23 00:01:00 00:01:15 01:00:00 10:00:00
This filter will display the time as word(s) indicating roughly the time of day ("Morning", "Afternoon", "Evening", etc). For example, the following template snippet: Posted in the {{ post.date|fuzzy_time }} of {{ post.date|date:"F j, Y"} }}. will result in the following (assuming `post.date == datetime.datetime(2007, 6, 13, 20, 57, 55, 765000)`): Posted in the evening of June 13, 2007. The terms used and breakpoints (hours only) can be rather arbitrary so you may want to adjust them to your liking. See the docs for [bisect][] for help in understanding the code. Just remember you should have one less breakpoint than periods and the first breakpoint falls at the end of the first period. The idea was inspired by [Dunstan Orchard][1], although the code is *very* different (php case statement). He uses quite a bit more periods in a day, so you might want to take a look. [bisect]: http://docs.python.org/lib/module-bisect.html [1]: http://www.1976design.com/blog/archive/2004/07/23/redesign-time-presentation/
This is tag similar to *timesince* and *timeuntil*, which work great until you starting giving timesince dates in the future or timeuntil dates in the past. Timedelta tag will humanize output correctly for both future and past dates using *now* as a default reference date (or a specified reference date as argument) now = Apr 27 2007 futuredate = May 5 2007 pastdate = Jan 5 2007 {{ futuredate|timedelta }} will output "in 1 week, 1 day" {{ pastdate|timedelta }} will output "3 months, 2 weeks ago"
This is quite a hack (need to modify Django code), but relatively simple and stable. It displays various times in whichever increments and columns you specify, rather than just Midnight, Noon, Now & 6am. You can use it throughout your admin and newforms-admin code. This is a (slightly updated) copy of the ticket #1848 which wasn't included in trunk. http://code.djangoproject.com/ticket/1848
Returns 'Morning', 'Afternoon', or 'Evening' in the local timezone (specified in settings). Required pytz. 0000-1200 considered morning, 1200-1800 considered afternoon, 1800-0000 considered evening.
This is a reasonably straight forward port of functionality provided by the `django.utils.dateformat` module into a method extending JavaScript's Date object. Its intended use is to allow client-side dynamic content to share the same date & time string formatting as Django template markup. By using this in conjunction with a context processor (to pass a format string to all templates) you can switch formats for both server-generated & client-generated dates across a complete site with a single setting. The function supports *almost* the entire format -- as listed by the Django documentation for the [now template tag](http://www.djangoproject.com/documentation/templates/#now) -- with the exception of "I" & "T". As a 'dumb' illustration, the following template tag usage: It is {% now "jS F Y H:i" %} ...could equate to the following: It is <script type="text/javascript">var now = new Date(); document.write(now.strfdate('jS F Y H:i'));</script> It's not extensively tested (I only wrote it over the weekend), but seems to be working okay. Feel free to leave any corrections or suggestions in the comments, and I'll try to keep this entry updated if I make any fixes or changes.
24 snippets posted so far.