我不知道如何將這種掛鉤到您的odoo
on_change
,但這裏是一個程序來從你的日期字符串加或減三個月:
代碼:
import datetime as dt
def add_month(date, months):
assert isinstance(date, dt.date)
year, month, day = date.year, date.month, date.day
year += int((month + months-1)/12)
month = (month + months - 1) % 12 + 1
return dt.date(year, month, day)
def add_month_from_string(date_string, months):
date = dt.datetime.strptime(date_string, '%d %b %Y')
return add_month(date, months).strftime('%d %b %Y')
測試數據:
test_data = (
("26 Feb 2017", 2, "26 Apr 2017"),
("6 Dec 2016", 3, "6 Mar 2017"),
("26 Feb 2017", -2, "26 Dec 2016"),
)
for have, inc, want in test_data:
print(have, inc, add_month_from_string(have, inc), want)
結果:
('26 Feb 2017', 2, '26 Apr 2017', '26 Apr 2017')
('6 Dec 2016', 3, '06 Mar 2017', '6 Mar 2017')
('26 Feb 2017', -2, '26 Dec 2016', '26 Dec 2016')