Skip to Content
Menu
This question has been flagged
2 Replies
14076 Views

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.           


Avatar
Discard
Best Answer

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 

Avatar
Discard
Best Answer

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

Avatar
Discard