【问题标题】:How can i check the time in Python [duplicate]我如何检查Python中的时间[重复]
【发布时间】:2023-11-27 04:35:01
【问题描述】:

如何在 Python 中查看时间并每天同时运行一个命令?

if datetime.time() == 17.30:
     bot.sendMessage(ChatID, "Message")

我在网上找到了很多,但我什么都没找到 这真的帮助了我。我喜欢查看小时和分钟。 稍后它应该被嵌入到 TelegramBot 中,它应该每天向群组发送一条消息。所以 Crontab 也不会是一个很好的计时器。

感谢您的帮助

【问题讨论】:

  • 如果你在 linux 上使用 crontab 可能会更好

标签: python bots telegram


【解决方案1】:

使用 cron 作业可能会更好地处理,但这里是代码

import datetime.datetime

now_time = datetime.now()

If now_time.hour == 17 and now_time.minute == 30:

    #do something

【讨论】: