我如何找到从现在开始的下周日中午 12 点的日期,然后加上 10 小时

How do I find what the date will be next Sunday 12 am from now and then add 10 hours to it

我有这个代码

today = datetime.now().date() 
# prints: 2022/1/14
rd = REL.relativedelta(days=1, weekday=REL.SU)
nextSunday = today + rd
#prints : 2022/1/16

如何在日期上加上 10 小时,以便我可以得到一个变量 nextSunday_10am,我可以将其减去当前时间

difference = nextSunday_10am - today 

并安排我需要做的事情

您可以使用 datetime.timedelta().

向 DateTime 添加小时数
nextSunday += datetime.timedelta(hours=10)

例如:

import datetime


today = datetime.datetime.today()
print("Today is "+str(today))
while today.weekday()+1 != 6: #0 = "Monday", 1 = "Tuesday"...
    today += datetime.timedelta(1)

nextSunday = today + datetime.timedelta(hours=10)
print("Next sunday +10hrs will be "+str(nextSunday))

您可以按照@Dani3le_ 的建议更直接地执行以下操作:

def getSundayTime(tme: datetime.date) -> datetime:
    nxt_sndy = tme + timedelta(days= 6 - tme.weekday())
    return datetime.combine(nxt_sndy, datetime.strptime('10:00', '%H:%M').time())

这将计算下一个星期日并将时间设置为10:00