Flask Snippets

Snippets are unofficial and unmaintained.

This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.

A Filter for the jQuery Timeago Plugin

Posted by Armin Ronacher on 2011-06-18 @ 01:33 and filed in JavaScript

Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. “4 minutes ago” or “about 1 day ago”). It automatically keeps them updated and only needs a very basic span tag or something similar with a certain class and title attribute.

For instance

<span class=timeago title="2008-07-17T09:24:17Z">...</span>

turns into something like this:

<span class=timeago title="July 17, 2008">2 years ago</span>

Because generating the HTML can be quite annoying to do by hand, here is a simple filter that does that for you:

def datetimeformat(datetime, timeago=True):
    readable = datetime.strftime('%Y-%m-%d @ %H:%M')
    if not timeago:
        return readable
    iso_format = datetime.strftime('%Y-%m-%dT%H:%M:%SZ')
    return '<span class=timeago title="%s">%s</span>' % (

And here is how you use it:

<p class=date>Date: {{ the_date|datetimeformat }}

Don't forget to include the Timeago plugin and to initialize it:

$(function() {

This snippet by Armin Ronacher can be used freely for anything you like. Consider it public domain.