Skip to content

Instantly share code, notes, and snippets.

@nzjrs
Created October 11, 2009 11:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nzjrs/207624 to your computer and use it in GitHub Desktop.
Save nzjrs/207624 to your computer and use it in GitHub Desktop.
Humanize date differences
#!/usr/bin/env python
# This function prints the difference between two python datetime objects
# in a more human readable form
#
# Adapted from: http://www.chimeric.de/blog/2008/0711_smart_dates_in_python
def humanize_date_difference(now, otherdate=None, offset=None):
if otherdate:
dt = otherdate - now
offset = dt.seconds + (dt.days * 60*60*24)
if offset:
delta_s = offset % 60
offset /= 60
delta_m = offset % 60
offset /= 60
delta_h = offset % 24
offset /= 24
delta_d = offset
else:
raise ValueError("Must supply otherdate or offset (from now)")
if delta_d > 1:
if delta_d > 6:
date = now + datetime.timedelta(days=-delta_d, hours=-delta_h, minutes=-delta_m)
return date.strftime('%A, %Y %B %m, %H:%I')
else:
wday = now + datetime.timedelta(days=-delta_d)
return wday.strftime('%A')
if delta_d == 1:
return "Yesterday"
if delta_h > 0:
return "%dh%dm ago" % (delta_h, delta_m)
if delta_m > 0:
return "%dm%ds ago" % (delta_m, delta_s)
else:
return "%ds ago" % delta_s
@danricho
Copy link

I believe that line 9 should be
dt = now - otherdate

Thanks for sharing the gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment