跳至內容
選單
此問題已被標幟
2 回覆
2241 瀏覽次數
@api.depends('join_date', 'todays_time')
def compute_exprience(self):
for rec in self:
if rec.join_date and rec.todays_time:
r1 = datetime.datetime.strptime(str(self.todays_time), '%Y-%m-%d')
r2 = datetime.datetime.strptime(str(self.join_date), '%Y-%m-%d')
rec.Experience = (r1.year - r2.year)


頭像
捨棄
最佳答案

Hi amina lifam,

Try,

experience = fields.Char(string='Experience', compute='compute_experience')

@api.depends('join_date')
def compute_experience(self):
for rec in self:
if rec.join_date:
join_date = rec.join_date
current_date = fields.Date.today()
years_diff = current_date.year - join_date.year
months_diff = current_date.month - join_date.month
if current_date.day < join_date.day:
months_diff -= 1
if months_diff < 0:
years_diff -= 1
months_diff += 12
experience_str = '{} years {} months'.format(years_diff, months_diff)
rec.experience = experience_str
else:
rec.experience = 'N/A'


Output:






Hope it helps,
Kiran K

頭像
捨棄
最佳答案

Try this:

from datetime import datetime

joining_date = datetime.strptime("join_date", "%Y-%m-%d")

today = datetime.today()

# Calculate total months and years
total_months = (today.year - joining_date.year) * 12 + (today.month - joining_date.month)
years, months = divmod(total_months, 12)

Then put it in rec.experience.....

Note: Don't use capital letter in fields like rec.Experience...



頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
1
6月 25
1777
3
7月 25
3381
1
5月 25
1530
1
5月 25
1788
4
5月 25
2919