我想将叶子持续时间的日期与odoo python中的当前日期进行比较

Ujj*_*hel 4 python datetime date openerp odoo-10

这是我编写并从hr.holidays继承的程序 ,如果所选日期在当前日期之前,则应提供错误消息。代码-

from datetime import date

if self.date_from <= date.today():
            print 'You cannot select the previous date'
Run Code Online (Sandbox Code Playgroud)

但它给出了错误 -

TypeError: can't compare datetime.date to bool
Run Code Online (Sandbox Code Playgroud)

谢谢

Er *_*yur 5

你好 Ujjwal Singh Baghel,

试试下面的代码,

#!/usr/bin/python
import datetime
i = datetime.datetime.now()

print ("Current date & time = %s" % i)


if self.date_from <= str(i):
            print 'You cannot select the previous date'
Run Code Online (Sandbox Code Playgroud)

或者

from datetime import date
if self.date_from <= str(date.today()):
            print 'You cannot select the previous date'
Run Code Online (Sandbox Code Playgroud)

例如

from datetime import date
if "10/07/2017" <= str(date.today()):
            print 'You cannot select the previous date'
Run Code Online (Sandbox Code Playgroud)

输出:

您不能选择上一个日期

我希望我的回答有帮助。如果有任何疑问,请发表评论。

  • 工作谢谢! (2认同)