I\'m having trouble formatting a datetime.timedelta
object.
Here\'s what I\'m trying to do: I have a list of objects and one of the members of the cl
I personally use the humanize library for this:
>>> import datetime
>>> humanize.naturalday(datetime.datetime.now())
'today'
>>> humanize.naturalday(datetime.datetime.now() - datetime.timedelta(days=1))
'yesterday'
>>> humanize.naturalday(datetime.date(2007, 6, 5))
'Jun 05'
>>> humanize.naturaldate(datetime.date(2007, 6, 5))
'Jun 05 2007'
>>> humanize.naturaltime(datetime.datetime.now() - datetime.timedelta(seconds=1))
'a second ago'
>>> humanize.naturaltime(datetime.datetime.now() - datetime.timedelta(seconds=3600))
'an hour ago'
Of course, it doesn't give you exactly the answer you were looking for (which is, indeed, str(timeA - timeB)
, but I have found that once you go beyond a few hours, the display becomes quickly unreadable. humanize
has support for much larger values that are human-readable, and is also well localized.
It's inspired by Django's contrib.humanize
module, apparently, so since you are using Django, you should probably use that.