Last active
May 24, 2021 09:07
-
-
Save bockor/190656c3e75fc9716d3d93fd3907bb8b to your computer and use it in GitHub Desktop.
python difference two dates 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
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