0

I have two dates (datetime), and I would like to make a condition in my view using these dates.

      date_start = object.date1
      date_end = object.date2
      now = datetime.datetime.now()

      if now >= date_start & now <=date_end :
            ...

I have this error: unsupported operand type(s) for &: 'datetime.datetime'. So I tried to add now = now.date(), but still doesn't work.

Any idea on how to do that? Thank you.

Juliette Dupuis
  • 1,007
  • 2
  • 13
  • 21

1 Answers1

5

Python uses and for boolean and:

if now >= date_start and now <= date_end:
    ....

It also supports a neat way of testing inequalities:

if date_start <= now <= date_end:
    ....
Tim
  • 11,009
  • 4
  • 39
  • 43