@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)
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Accounting
- Inventory
- PoS
- Project
- MRP
This question has been flagged
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...
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign upRelated Posts | Replies | Views | Activity | |
---|---|---|---|---|
|
1
Jun 25
|
1796 | ||
|
3
Jul 25
|
3394 | ||
|
1
May 25
|
1542 | ||
|
1
May 25
|
1803 | ||
|
4
May 25
|
2926 |