Skip to content

Instantly share code, notes, and snippets.

@lindslev
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save lindslev/8891302 to your computer and use it in GitHub Desktop.

Select an option

Save lindslev/8891302 to your computer and use it in GitHub Desktop.
import datetime
compare_date = datetime.datetime(2014, 2, 8, 18, 5, 1)
def timeAgo(time):
current_time = datetime.datetime.now().time()
today = datetime.date.today()
current_month = today.month
current_year = today.year
total_current_time = (current_year-1)*525949 + (current_month-1)*43829 + today.day*1440 + current_time.hour*60 + current_time.minute
total_passed_time = (time.year-1)*525949 + (time.month-1)*43829 + time.day*1440 + time.hour*60 + time.minute
total_diff = total_current_time - total_passed_time
if total_diff < 0:
return "What? That's the future."
if total_diff <= 1:
return "Just now"
elif total_diff <= 59:
return "%d minute(s) ago" % total_diff
elif total_diff <= 1439:
to_return = total_diff / 60
return "%d hour(s) ago" % to_return
elif total_diff <= 10079:
to_return = (total_diff / 60) / 24
return "%d day(s) ago" % to_return
elif total_diff <= 43828:
to_return = (((total_diff / 60) / 24) / 7)
return "%d week(s) ago" % to_return
else:
to_return = total_diff / 43829
return "%d month(s) ago" % to_return
print timeAgo(compare_date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment