如何在odoo中获得总年,月和日

时间:2018-06-18 14:15:24

标签: python

我的例子:

打印返回

  

年= 1个月= 5天= 15

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

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

1 个答案:

答案 0 :(得分:1)

尝试使用dateutil

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)