import datetime
difference = (datetime.date(2020, 1, 15) - datetime.date(1987, 7, 15))
'''This gives return as days but I want this as [year, month, day]
format. The result must be accurate. '''
run
pip install python-dateutil
try the below code
from datetime import datetime
from dateutil import relativedelta
date1 = datetime(2020, 1, 15)
date2 = datetime(1987, 7, 15)
diff = relativedelta.relativedelta(date1, date2)
years = diff.years
months = diff.months
days = diff.days
print('{},{},{} '.format(years, months, days))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.