解决这个问题的方法是将时间转换为以秒为单位,然后进行比较。
以下是一个示例代码:
import datetime
time1 = datetime.datetime.now()
time2 = datetime.datetime.now()
# 将时间转换为秒
seconds1 = time1.total_seconds()
seconds2 = time2.total_seconds()
# 比较两个时间的秒数
if seconds1 > seconds2:
print("time1 晚于 time2")
else:
print("time1 早于或等于 time2")
这个代码示例中,我们使用datetime
模块来获取当前时间。然后,使用total_seconds()
方法将时间转换为秒数。最后,我们比较这两个秒数来判断时间的先后关系。