import datetime
difference = (datetime.date(2020, 1, 15) - datetime.date(1987, 7, 15))
'''这将以天为单位返回,但我希望此格式为[year, month, day]
。结果必须准确。 '''
答案 0 :(得分:0)
运行
pip install python-dateutil
尝试以下代码
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))