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

Time:01-15

I have this code

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

How do i add 10 hours to the date so i can get a variable nextSunday_10am that i can substract to the current time

difference = nextSunday_10am - today 

and schedule what I need to do

CodePudding user response:

You can do the same thing as suggested by @Dani3le_ more directly with the following:

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())

This will compute calculate the next Sunday and set time to 10:00

CodePudding user response:

You can add hours to a DateTime by using datetime.timedelta().

nextSunday  = datetime.timedelta(hours=10)

For example:

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))
  •  Tags:  
  • Related