Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
25867 มุมมอง

I need to calculate the difference between two dates : 

py :

start_date = field.Date()

end_date = field.Date()

xml :

<field name="start_date"/>

<field name="end_date"/>



อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hello Yassin,

Try something like this from your python file.

You can call the function in different ways.

Check an example from Button Click.

from datetime import datetime


class YourClass(models.Model):
_inherit = 'your.model'

@api.multi
def your_function(self):
fmt = '%Y-%m-%d'
start_date = self.start_date
end_date = self.end_date
d1 = datetime.strptime(start_date, fmt)
d2 = datetime.strptime(end_date, fmt)
date_difference = d2 - d1


Thanks & Regards

Avinash N K

อวตาร
ละทิ้ง
ผู้เขียน

hi Avinash, thanks for your answer

I try to call the function in a compute filds and display the result but it doesn't work this is my code, it dispays 0 :

...

class EcoStage(models.Model):

name =eco.stage

start_date = fields.Date()

end_date = fields.Date()

duree_stage = fields.Integer(string="duree stage", compute='difference_date', store=True)

@api.multi

def difference_date(self):

fmt = '%Y-%m-%d'

start_date = self.start_date

end_date = self.end_date

d1 = datetime.strptime(start_date, fmt)

d2 = datetime.strptime(end_date, fmt)

date_difference = d2 - d1

in xml :

<fields name="duree_stage "/>

Add the 2 fields in the dependency.

---> @api.depends('start_date', 'end_date')

Also d2-d1 not gives an integer value.

So do like this. (d2 - d1).days

And assign the difference value in the computed field.

Like this. self.duree_stage = (d2 - d1).days

ผู้เขียน

thanks a lot Avinash

คำตอบที่ดีที่สุด

It can be done easily in odoo 14:

start_date = fields.Date(string="Start date")
end_date = fields.Date(string="End date")
date_diff = fields.Char("Date difference")

@api.constrains('start_date', 'end_date', 'date_diff')
def _date_difference(self):
for record in self:
if record.start_date > record.end_date:
raise ValidationError('Start date should not greater than end date.')
else:
record.date_diff = (record.end_date - record.start_date).days



อวตาร
ละทิ้ง
ผู้เขียน คำตอบที่ดีที่สุด

finally it works 

thanks a lot Avinash you save my life

this is the entier final code :

in .py:

...

class EcoStage(models.Model):

_name ='eco.stage'

start_date = fields.Date()

end_date = fields.Date()

duree_stage = fields.Integer(string="duree stage", compute='difference_date', store=True)

@api.multi

@api.depends('  start_date ',' end_date ')

def difference_date(self):

fmt = '%Y-%m-%d'

start_date = self.start_date

end_date = self.end_date

d1 = datetime.strptime(start_date, fmt)

d2 = datetime.strptime(end_date, fmt)

if d2 >d1 :

self.  duree_stage = (d2 - d1).days

else :

raise ValueError('end date should be superior than start day')

...

in xml :

<fields name="duree_stage "/>


อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
พ.ย. 22
3226
1
ส.ค. 15
5372
Year only needed - how to implement? แก้ไขแล้ว
2
มี.ค. 15
5277
4
ก.ค. 24
32317
2
มี.ค. 15
5509