本文介绍了计算从现在到今天或明天的指定时间的秒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我本以为这个问题已经有了答案,但似乎没有.在 Python 中,我想计算从现在"到今天"或明天"指定时间的秒数,无论何时发生(例如,下一次发生在 6:30 AM).计划是倒计时秒数并在指定时间触发音乐警报.我对 Python 还很陌生,到目前为止,datetime
文档并没有帮助我解决这个问题.有人可以给我一些帮助吗?
I would have thought this question would already have an answer, but it doesn't seem to. In Python, I want to calculate the number of seconds from "now" to a specified time "today" or "tomorrow," whenever it next occurs (e.g., the next occurrence of 6:30 AM). The plan is to count down seconds and set off a musical alarm at the specified time. I'm pretty new at Python, and the datetime
documentation hasn't helped me with this so far. Can somebody give me some assistance with this?
推荐答案
这应该可以:
from datetime import datetime, timedelta
now = datetime.now()
(timedelta(hours=24) - (now - now.replace(hour=6, minute=30, second=0, microsecond=0))).total_seconds() % (24 * 3600)
这篇关于计算从现在到今天或明天的指定时间的秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!