Created
February 13, 2018 18:11
-
-
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
This file contains hidden or 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
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