从当前时间快进到早上 7 点,但还没有找到这样做的方法
fast forward from current time to 7am and have not found a way to do so
from datetime import datetime
print("The time is",datetime.now(),"and you should sleep now")
当前时间的示例是 2017-12-16 21:24:02.620893,我希望时间快进到第二天早上 7 点。我该怎么做?
使用timedelta()
将一天提前到明天,然后使用replace()
设置小时、分钟和秒。
from datetime import datetime, timedelta
tomorrow_now = datetime.now() + timedelta(days=1)
tomorrow_7am = tomorrow_now.replace(hour=7, minute=0, second=0, microsecond=0)
您可以跳过 timedelta()
并只对所有内容使用 replace()
,但您需要对每月的最后一天进行特殊处理。
from datetime import datetime
print("The time is",datetime.now(),"and you should sleep now")
当前时间的示例是 2017-12-16 21:24:02.620893,我希望时间快进到第二天早上 7 点。我该怎么做?
使用timedelta()
将一天提前到明天,然后使用replace()
设置小时、分钟和秒。
from datetime import datetime, timedelta
tomorrow_now = datetime.now() + timedelta(days=1)
tomorrow_7am = tomorrow_now.replace(hour=7, minute=0, second=0, microsecond=0)
您可以跳过 timedelta()
并只对所有内容使用 replace()
,但您需要对每月的最后一天进行特殊处理。