Skip to content

Instantly share code, notes, and snippets.

@kostyll
Created April 20, 2015 15:54
Show Gist options
  • Save kostyll/bc74ac1c962c964e2b2b to your computer and use it in GitHub Desktop.
Save kostyll/bc74ac1c962c964e2b2b to your computer and use it in GitHub Desktop.
test gist
from datetime import datetime
def yearsago(years, from_date=None):
if from_date is None:
from_date = datetime.now()
try:
return from_date.replace(year=from_date.year - years)
except:
# Must be 2/29!
assert from_date.month == 2 and from_date.day == 29 # can be removed
return from_date.replace(month=2, day=28,
year=from_date.year-years)
def num_years(begin, end=None):
if end is None:
end = datetime.now()
num_years = int((end - begin).days / 365.25)
if begin > yearsago(num_years, end):
return num_years - 1
else:
return num_years
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment