如何使用时间字符串检查当前时间是否在 python 范围内?

How to check if the current time is in range in python using time strings?

我需要一种方法来使用时间字符串查明当前时间是否在时间范围内 window。因此,例如开始时间为“0:00”,结束时间为“3:15”。

我遇到了这个解决方案,How to check if the current time is in range in python?,它很棒,但它不使用时间字符串。 python 中有什么我可以传递两个时间字符串然后检查它们之间是否有东西吗?还有其他建议吗?

谢谢。

这是time模块的一个有用的方法: https://docs.python.org/2/library/time.html#time.strptime

这是它的用法示例:

import time
new_time = time.strptime("0:00", "%H:%M")
other_time = time.strptime("3:15", "%H%M")

print(new_time > other_time)
print(other_time > new_time)

不太确定您使用的 Python 是哪个版本,所以我在 python3 中进行了处理。

import time

old_time = time.strptime('01:00', '%H:%M')
new_time = time.strptime('06:00', '%H:%M')

print(old_time)
print(new_time)

if old_time > new_time:
    print('old time is old')
else:
    print('new is cool')