获取两个日期之间的差异的[年月日]

时间:2019-12-07 04:32:50

标签: python python-3.x datetime

import datetime

difference = (datetime.date(2020, 1, 15) - datetime.date(1987, 7, 15))

'''这将以天为单位返回,但我希望此格式为[year, month, day]。结果必须准确。 '''

1 个答案:

答案 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))