如何检查时间是否在两天之间的范围内? [英] How to check if time is in the range between two days?

查看:100
本文介绍了如何检查时间是否在两天之间的范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一些很好的例子来检查,如果时间在特定范围内,就像这个:

I found some nice examples to check, if a time is in a specific range, like this one:

now_time = datetime.datetime.now().time()   
start = datetime.time(17, 30)
end = datetime.time(4, 00)
if start <= now_time <= end:

就我而言,我想检查当前时间是否在早上 17:30 到 04 之间.我该如何解决这个问题?

In my case, I want to check, if the current time is between 17:30 and 04 in the morning. How am I able to solve this?

推荐答案

17:30 出现在 4:00 之后,所以任何带有 start <= x <= end 的内容都将评估为 false,因为这意味着 end (4:00) 大于 start (17:30),这永远不会是真的.

17:30 comes after 4:00, so anything with start <= x <= end will evaluate to false, because it implies that end (4:00) is larger than start (17:30), which is never true.

你必须做的是检查它是超过 17:30 还是在 4:00 之前:

What you must do instead is check whether it's past 17:30 or it's before 4:00:

import datetime

now_time = datetime.datetime.now().time()
start = datetime.time(17, 30)
end = datetime.time(4, 00)
if now_time >= start or now_time <= end:
    print('true')
else:
    print('false')

这篇关于如何检查时间是否在两天之间的范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆