Nicolas Font
Nicolas Font

Reputation: 65

How to print Facebook style timestamps in Django

I have a datetime that represents an event creation time and I need to show how much time has passed since then.

Examples: “right now”, “2 days ago”, “3 months from now”.

I know of PrettyTime for Java, but I need something for Django.

Thanks!

Upvotes: 1

Views: 584

Answers (2)

Josh Hunt
Josh Hunt

Reputation: 14501

Not sure if it is exactly what you are looking for, but Django does have a built in template filter that achieves something similar: timesince

Formats a date as the time since that date (e.g., "4 days, 6 hours").

Takes an optional argument that is a variable containing the date to use as the comparison point (without the argument, the comparison point is now). For example, if blog_date is a date instance representing midnight on 1 June 2006, and comment_date is a date instance for 08:00 on 1 June 2006, then {{ blog_date|timesince:comment_date }}` would return "8 hours".

Upvotes: 2

sahhhm
sahhhm

Reputation: 5365

Py-Pretty should do the trick... It also has support for future events as well.

Upvotes: 4

Related Questions