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
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
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.
You could write extension methods for the DateTime class to provide this feature for you.
public static class DateTimeExtensions
{
public static bool Between(this DateTime current, DateTime startTime, DateTime endTime)
{
return startTime <= currentTime <= endTime;
}
public static bool Within(this DateTime current, DateTime startTime, DateTime endTime)
{
return startTime < currentTime < endTime;
}
}
Then you have a nice syntax like:
if (current_time.Within(my_event.start_time, my_event.end_time))
{
}
you can use a simple if comparing three dates, like this
if date1 < yourdate < date2:
...do something...
else:
...do ...