Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 从当前时间快进到早上7点,但还没有找到这样做的方法_Python 3.x_Datetime_Raspberry Pi3 - Fatal编程技术网

Python 3.x 从当前时间快进到早上7点,但还没有找到这样做的方法

Python 3.x 从当前时间快进到早上7点,但还没有找到这样做的方法,python-3.x,datetime,raspberry-pi3,Python 3.x,Datetime,Raspberry Pi3,当前时间的一个例子是2017-12-16 21:24:02.620893,我希望时间快进到第二天早上7点。我该怎么做?使用timedelta()将一天提前到明天,然后使用replace()设置小时、分钟和秒 from datetime import datetime print("The time is",datetime.now(),"and you should sleep now") 您可以跳过使用timedelta()而只使用replace()进行所有操作,但您需要对每月的最后一天进行

当前时间的一个例子是2017-12-16 21:24:02.620893,我希望时间快进到第二天早上7点。我该怎么做?

使用
timedelta()
将一天提前到明天,然后使用
replace()
设置小时、分钟和秒

from datetime import datetime
print("The time is",datetime.now(),"and you should sleep now")

您可以跳过使用
timedelta()
而只使用
replace()
进行所有操作,但您需要对每月的最后一天进行特殊处理。

日期时间从系统时钟读取,因此您必须进行更改。是否要显示明天上午7点的日期和时间,或者你想让系统时钟实际设置为那个时间?从任何时间开始,程序在第二天早上7点显示,就好像夜晚被跳过一样。我刚刚注意到我的答案与这个答案相同,但你的答案是在几分钟前上传的。我删除了我的答案,但用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)