如何检查给定的日期时间对象是否在两个日期时间之间?

时间:2022-12-28 21:28:44
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

4 个解决方案

#1


6  

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.

只要start_time和end_time没有关联的tzinfo类,您的答案就是前进的方法。您无法直接将天真日期时间与timezoned-datetime进行比较。

#2


3  

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

你可以使用简单的比较三个日期,比如这个

if date1 < yourdate < date2:
  ...do something...
else:
  ...do ...

#3


1  

I know old, but since this is so high on Google results, answers here don't take into consideration two cases:

我知道年纪大了,但由于Google搜索结果如此之高,这里的答案并没有考虑到两种情况:

  1. If your time equals either of your range, ie your range is 6-8 and it is 6.
  2. 如果你的时间等于你的任何一个范围,即你的范围是6-8而它是6。

  3. If your time range is say 18:00 to 6:00, valid range; however 19:00 would not match.
  4. 如果您的时间范围是18:00到6:00,则有效范围;但是19:00不匹配。

I wrote a function to take care of time comparison, hope this helps anyone viewing this old question.

我写了一个函数来处理时间比较,希望这有助于任何人查看这个老问题。

def process_time(intime, start, end):
    if start <= intime <= end:
        return True
    elif start > end:
        end_day = time(hour=23, minute=59, second=59, microsecond=999999)
        if start <= intime <= end_day:
            return True
        elif intime <= end:
            return True
    return False

#4


0  

The datetimes getting tested need to all naive (no timezone) or all aware (timezone). An exception should occur if you try to compare aware and naive. If all the datetimes are aware the timezones don't actually have to match that appears to be taken into consideration when comparing.

进行测试的日期时间需要所有天真(无时区)或全部知晓(时区)。如果您尝试比较意识和天真,则应该发生异常。如果所有日期时间都知道时区实际上不必匹配,那么在比较时似乎需要考虑这些时区。

e.g.

class RND(datetime.tzinfo):
    """ Random timezone UTC -3 """

    def utcoffset(self, dt):
        return datetime.timedelta(hours=-3)

    def tzname(self, dt):
        return "RND"

    def dst(self, dt):
        return datetime.timedelta(hours=0)


april_fools = datetime.datetime(year=2017, month=4, day=1, hour=12, tzinfo=pytz.UTC)

random_dt = datetime.datetime(year=2017, month=4, day=1, hour=9, tzinfo=RND())

random_dt == april_fools
# True as the same time when converted back to utc.

# Between test of 3 naive datetimes
start_spring = datetime.datetime(year=2018, month=3, day=20)
end_spring = datetime.datetime(year=2018, month=6, day=21)
april_fools = datetime.datetime(year=2018, month=4, day=1)


if start_spring < april_fools < end_spring:
    print "April fools is in spring"

#1


6  

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.

只要start_time和end_time没有关联的tzinfo类,您的答案就是前进的方法。您无法直接将天真日期时间与timezoned-datetime进行比较。

#2


3  

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

你可以使用简单的比较三个日期,比如这个

if date1 < yourdate < date2:
  ...do something...
else:
  ...do ...

#3


1  

I know old, but since this is so high on Google results, answers here don't take into consideration two cases:

我知道年纪大了,但由于Google搜索结果如此之高,这里的答案并没有考虑到两种情况:

  1. If your time equals either of your range, ie your range is 6-8 and it is 6.
  2. 如果你的时间等于你的任何一个范围,即你的范围是6-8而它是6。

  3. If your time range is say 18:00 to 6:00, valid range; however 19:00 would not match.
  4. 如果您的时间范围是18:00到6:00,则有效范围;但是19:00不匹配。

I wrote a function to take care of time comparison, hope this helps anyone viewing this old question.

我写了一个函数来处理时间比较,希望这有助于任何人查看这个老问题。

def process_time(intime, start, end):
    if start <= intime <= end:
        return True
    elif start > end:
        end_day = time(hour=23, minute=59, second=59, microsecond=999999)
        if start <= intime <= end_day:
            return True
        elif intime <= end:
            return True
    return False

#4


0  

The datetimes getting tested need to all naive (no timezone) or all aware (timezone). An exception should occur if you try to compare aware and naive. If all the datetimes are aware the timezones don't actually have to match that appears to be taken into consideration when comparing.

进行测试的日期时间需要所有天真(无时区)或全部知晓(时区)。如果您尝试比较意识和天真,则应该发生异常。如果所有日期时间都知道时区实际上不必匹配,那么在比较时似乎需要考虑这些时区。

e.g.

class RND(datetime.tzinfo):
    """ Random timezone UTC -3 """

    def utcoffset(self, dt):
        return datetime.timedelta(hours=-3)

    def tzname(self, dt):
        return "RND"

    def dst(self, dt):
        return datetime.timedelta(hours=0)


april_fools = datetime.datetime(year=2017, month=4, day=1, hour=12, tzinfo=pytz.UTC)

random_dt = datetime.datetime(year=2017, month=4, day=1, hour=9, tzinfo=RND())

random_dt == april_fools
# True as the same time when converted back to utc.

# Between test of 3 naive datetimes
start_spring = datetime.datetime(year=2018, month=3, day=20)
end_spring = datetime.datetime(year=2018, month=6, day=21)
april_fools = datetime.datetime(year=2018, month=4, day=1)


if start_spring < april_fools < end_spring:
    print "April fools is in spring"