判断时间是否在时间区间内

>>> from datetime import datetime
>>> a = datetime.now()
>>> b = datetime.now()
>>> a
datetime.datetime(2019, 12, 4, 17, 3, 32, 396329)
>>> b
datetime.datetime(2019, 12, 4, 17, 3, 40, 66766)
>>> a < b
True
>>> b < a
False

我们可以直接用连等式来进行比较:

>>> from dateutil.parser import parse
>>> from datetime import datetime
>>> NOW = datetime.now()
>>> NOW
datetime.datetime(2019, 12, 4, 17, 3, 40, 66766)
>>> gt = parse("2019-12-1")
>>> lt = parse("2019-12-5")
>>> gt< NOW < lt
True

上面是一些简单的比较,现在我们看一下区间比较的库:

>>> from interval import Interval
>>> a = Interval("2019-06-11 13:00", "2019-06-11 19:00")
>>> b = Interval("2019-06-11 14:00", "2019-06-11 17:00")
>>> a in b
False
>>> b in a
True