4

Here is the program which i wrote and inherited from hr.holidays that if the selected date is before the current date then it should provide an error message. Code-

from datetime import date

if self.date_from <= date.today():
            print 'You cannot select the previous date'

But it gives the error-

TypeError: can't compare datetime.date to bool

Thanks

Ujjwal Singh Baghel
  • 393
  • 2
  • 6
  • 23

1 Answers1

5

Hello Ujjwal Singh Baghel,

Try this below code,

#!/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'

OR

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

For example

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

Out put:

You cannot select the previous date

I hope my answer is helpful. If any query so comments, please.

Mayur Vora
  • 922
  • 2
  • 14
  • 25