Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a given datetime object is "between" two datetimes?

my_event = Event.objects.get(id=4)
current_time = datetime.datetime.now()

How do I do check if my current time is between them?

my_event.start_time < current_time < my_event.end_time
like image 966
TIMEX Avatar asked Apr 14 '10 00:04

TIMEX


2 Answers

Your answer is the way to go as long as start_time and end_time don't have an associated tzinfo class. You can't directly compare a naive datetime with a timezoned-datetime.

like image 52
Jeffrey Harris Avatar answered Oct 23 '22 16:10

Jeffrey Harris


you can use a simple if comparing three dates, like this

if date1 < yourdate < date2:
  ...do something...
else:
  ...do ...
like image 24
eos87 Avatar answered Oct 23 '22 16:10

eos87