如何使用datetim设置特定时间

2024-04-23 23:06:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想确定具体时间

我已经开始了

def function():
    current_time = datetime.datetime.time(datetime.datetime.now()).isoformat()[0:8]
    schedule = datetime.time(10, 00, 00)
    if current_time == schedule:

我想看看现在的时间是不是10点,有没有更好的办法?你知道吗


Tags: datetimeiftimedef时间functioncurrentnow
2条回答
import datetime

dt = datetime.datetime.now()

if dt.hour == 10 and dt.minute == 0:
    print "it's time"

不需要那么复杂的代码。你知道吗

current_time = datetime.datetime.now().time()
schedule = datetime.time(10, 00, 00)
current_time == schedule

尽管这只会在10:00:00的精确微秒内发生,但这几乎肯定不是你想要的。你要不要检查一下是在十点钟吗?那就更简单了:

datetime.datetime.now().hour == 10

相关问题 更多 >