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

hello 

HOW I CAN CALCULATE DIFFERENCE BETWEEN TWO DATE

for record in self:

    record['x_studio_total_jours'] = record['x_studio_date_departe'] - record['x_studio_date_arrive']


the code is correct ?

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

Thank you 
it working

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

Hi 

Try this code 

for record in self:

  record['x_studio_total_days'] = 0

  if record['x_studio_date_arrive'] and record['x_studio_date_departure']:

    record['x_studio_total_days'] = (record['x_studio_date_departure'] - record['x_studio_date_arrive']).days

    


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

Hi,

Refer to the code

from datetime import datetime

for record in self:
if record['x_studio_date_departe'] and record['x_studio_date_arrive']:
depart_date = fields.Datetime.from_string(record['x_studio_date_departe'])
arrive_date = fields.Datetime.from_string(record['x_studio_date_arrive'])

difference = (arrive_date - depart_date).days
record['x_studio_total_jours'] = difference
else:
record['x_studio_total_jours'] = 0 # Handle the case where one or both dates are not set

In this code:

  1. We check if both x_studio_date_departe and x_studio_date_arrive fields have values
  2. We use the fields.Datetime.from_string() method to convert the date strings to datetime objects.
  3. We calculate the difference between the two dates and assign it to x_studio_total_jours.
  4. We also handle the case where one or both dates are not set by setting x_studio_total_jours to 0 in that case

Make sure you have the fields module imported in your file for this code to work properly. 

Hope it helps

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
2
เม.ย. 21
3289
3
ก.พ. 24
2757
Calculate days between two dates แก้ไขแล้ว
1
ก.พ. 24
4982
0
เม.ย. 21
4042
2
ก.ค. 25
424