跳至內容
選單
此問題已被標幟
2 回覆
14908 瀏覽次數

Hi all,

    Assuming that I have two values:

    StartDate = fields.Datetime()
    EndDate = fields.Datetime()

   And now I'm supposed to let the"EndDate"not earlier than "StartDate": if it happens,I will receive a warning.

So how to compare these dates become a problem.

   Thanks.           


頭像
捨棄
最佳答案

Hello, 

Try Below code maybe it helps you

@api.multi
def get_two_date_comp(self):
start_date = self.StartDate.strftime('%Y-%m-%d')
end_date = self.EndDate.strftime('%Y-%m-%d')
if start_date > end_date:
raise ValidationError("Validation message")

Thanks 

頭像
捨棄
最佳答案

Hello,

You can also try this code

@api.api
def check_dates(self):
start_date = fields.Date.from_string(self.StartDate)
end_date = fields.Date.from_string(self.EndDate)
if start_date > end_date:
raise ValidationError(_("End Date cannot be set before Start Date."))

頭像
捨棄