Skip to content

Instantly share code, notes, and snippets.

@stucka
Created February 13, 2018 18:11
Show Gist options
  • Save stucka/1bf61296a214feffcb1872681132efba to your computer and use it in GitHub Desktop.
Save stucka/1bf61296a214feffcb1872681132efba to your computer and use it in GitHub Desktop.
Given Google Sheet date serial and a target date (election?) calculate difference in years
def calculate_age(dobstring, targetdate):
import datetime
# Takes Google Sheets (Excel)-formatted date value, and YYYY-MM-DD formatted date.
dobobject = datetime.datetime(1899, 12, 30) + datetime.timedelta(days=dobstring)
# print(dobobject)
targetobject = datetime.datetime.strptime(targetdate, "%Y-%m-%d")
years = targetobject.year - dobobject.year
if targetobject.month < dobobject.month or (targetobject.month == dobobject.month and targetobject.day < dobobject.day):
years -= 1
return(years)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment