Python 如何获取odoo中的总年、月和日

Python 如何获取odoo中的总年、月和日,python,Python,我的例子是: 打印报税表 年=1个月=5天=15 如何获得2年、6个月和16天的差异?尝试使用: 你为什么想要2年6个月?正如我认为你没有调整你电脑的时钟一样,从2017-01-01(你的基准日期)到今天(2018-06-18),已经过去了1年5个月17天。我想你问了16天,因为你昨天(6月17日)测试了它…这和这个问题有什么关系?Tnx的帮助,你知道任何简单的例子如何转换例如370天的格式(1年,0个月,5天)Tnx! import datetime import math new_date

我的例子是:

打印报税表

年=1个月=5天=15

如何获得2年、6个月和16天的差异?

尝试使用:


你为什么想要2年6个月?正如我认为你没有调整你电脑的时钟一样,从2017-01-01(你的基准日期)到今天(2018-06-18),已经过去了1年5个月17天。我想你问了16天,因为你昨天(6月17日)测试了它…这和这个问题有什么关系?Tnx的帮助,你知道任何简单的例子如何转换例如370天的格式(1年,0个月,5天)Tnx!
import datetime
import math
new_date = datetime.datetime(2017,1,1)
differnce = datetime.datetime.now() - new_date
year = differnce.days//(365.25)
month = (differnce.days-year*365.25)//(365.25/12)
day = ((differnce.days-year*365.25) - month*(365.25/12))
print('Years=',int(year),' Months=',int(month), ' Days=',int(math.ceil(day)))

year_2 = 1
month_2 = 1
days_2 = 1
import datetime
from dateutil.relativedelta import relativedelta

new_date = datetime.datetime(2017,1,1)
difference = relativedelta(datetime.datetime.now(), new_date)
difference
# relativedelta(years=+1, months=+5, days=+17, hours=+10, minutes=+22, seconds=+36, microseconds=+281175)

new_difference = difference + relativedelta(years=1, months=1, days=1)
new_difference
# relativedelta(years=+2, months=+6, days=+18, hours=+10, minutes=+22, seconds=+36, microseconds=+281175)