Last active
August 29, 2015 13:56
-
-
Save lindslev/8859663 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
compare_date = datetime.datetime(2014, 2, 7, 19, 44, 1) | |
def timeAgo(time): | |
current_time = datetime.datetime.now().time() | |
today = datetime.date.today() | |
current_month = today.month | |
current_year = today.year | |
#would i be allowed to write another function? hmm | |
#current_week = whatWeekIs(today) | |
#then_week = whatWeekIs(time.day) | |
if today.day <= 7: | |
current_week = 1 | |
elif today.day > 7 and today.day <= 14: | |
current_week = 2 | |
elif today.day > 14 and today.day <= 21: | |
current_week = 3 | |
else: | |
current_week = 4 | |
if time.day <= 7: | |
then_week = 1 | |
elif time.day > 7 and time.day <= 14: | |
then_week = 2 | |
elif time.day > 14 and time.day <= 21: | |
then_week = 3 | |
else: | |
then_week = 4 | |
if current_year != time.year: | |
year_diff = current_year - time.year #1 | |
if current_month < time.month: | |
months_to_add = (12 - (time.month - current_month)) + (year_diff - 1)*12 | |
else: | |
months_to_add = (current_month - time.month) + year_diff*12 | |
return "%d months ago" % months_to_add | |
else: | |
if current_month == time.month: | |
if current_week == then_week: | |
if today.day == time.day: | |
if current_time.hour == time.hour and current_time.minute == time.minute: | |
return "Just now" | |
if current_time.hour == time.hour and current_time.minute > time.minute: | |
to_return = current_time.minute - time.minute | |
return "%d minutes ago" % to_return | |
if current_time.hour != time.hour: | |
to_return = current_time.hour - time.hour | |
return "%d hours ago" % to_return | |
else: | |
day_diff = today.day - time.day | |
return "%d days ago" % day_diff | |
elif (today.day - time.day) < 7: | |
day_diff = today.day - time.day | |
return "%d days ago" % day_diff | |
else: | |
week_diff = current_week - then_week | |
return "%d weeks ago" % week_diff | |
else: | |
if current_week < then_week: | |
if then_week == 4 and current_week == 1: | |
num_of_days = 7 + (30 - time.day) | |
if num_of_days < 14: | |
return "%d days ago" % num_of_days | |
elif num_of_days > 14 and num_of_days < 20: | |
return "2 weeks ago" | |
else: | |
week_diff = 4 - (then_week - current_week) | |
return "%d weeks ago" % week_diff | |
else: | |
month_diff = current_month - time.month | |
return "%d months ago" % month_diff | |
print time_ago(compare_date) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment