Se rendre au contenu
Menu
Cette question a été signalée
2 Réponses
2238 Vues
@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)


Avatar
Ignorer
Meilleure réponse

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

Avatar
Ignorer
Meilleure réponse

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...



Avatar
Ignorer
Publications associées Réponses Vues Activité
1
juin 25
1777
3
juil. 25
3381
1
mai 25
1530
1
mai 25
1788
4
mai 25
2919