Skip to content

Instantly share code, notes, and snippets.

@bockor
Last active May 24, 2021 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bockor/190656c3e75fc9716d3d93fd3907bb8b to your computer and use it in GitHub Desktop.
Save bockor/190656c3e75fc9716d3d93fd3907bb8b to your computer and use it in GitHub Desktop.
python difference two dates in years
import datetime
d1_date_str='29/08/1962'
d2_date_str= '06/06/1968'
d1_date_obj=datetime.datetime.strptime(d1_date_str, '%d/%m/%Y')
print(d1_date_obj.date())
d2_date_obj=datetime.datetime.strptime(d2_date_str, '%d/%m/%Y')
print(d2_date_obj.date())
#create datetime.timedelta object
diff = d2_date_obj - d1_date_obj
print(type(diff))
diff_days = diff.days
print(diff_days)
diff_years = round(diff_days/365)
print(diff_years)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment