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,027
  • 2
  • 13
  • 22

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,710
  • 4
  • 42
  • 43
  • Thank you very much! Now I get this error: "can't compare offset-naive and offset-aware datetimes". But I guess it's an other issue. – Juliette Dupuis Nov 22 '12 at 09:04
  • @JulietteDupuis: Maybe http://stackoverflow.com/questions/10652819/django-1-4-cant-compare-offset-naive-and-offset-aware-datetimes can help you with that. – Tim Nov 22 '12 at 09:05
  • Yes indeed! It solved my problem. Thank you again, I'll mark this answer. – Juliette Dupuis Nov 22 '12 at 09:07