Skip to content

Instantly share code, notes, and snippets.

@helpse
Created May 24, 2014 04:36
Show Gist options
  • Save helpse/87f6a4167df4b97f9dc1 to your computer and use it in GitHub Desktop.
Save helpse/87f6a4167df4b97f9dc1 to your computer and use it in GitHub Desktop.
import datetime
def prettydate(d):
diff = datetime.datetime.utcnow() - d
s = diff.seconds
if diff.days > 7 or diff.days < 0:
return d.strftime('%d %b %y')
elif diff.days == 1:
return '1 day ago'
elif diff.days > 1:
return '{} days ago'.format(diff.days)
elif s <= 1:
return 'just now'
elif s < 60:
return '{} seconds ago'.format(s)
elif s < 120:
return '1 minute ago'
elif s < 3600:
return '{} minutes ago'.format(s/60)
elif s < 7200:
return '1 hour ago'
else:
return '{} hours ago'.format(s/3600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment