Skip to content

@nzjrs /datedifference.py
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.