'Find the date of a given day, from today in python
Let's say today is Thursday, 02-03-2022 in mm/dd/yr format. I want to find out what date it will be next Sunday. Which should be 02-06-2022.
Something like:
def return_date(day):
return date_of_day
date = return_date('sunday')
Solution 1:[1]
Next Sunday can be found by adding the difference between (Sunday, day_of_week==6) and today (Thursday, day_of_week == 3)
from datetime import date, timedelta
from operator import itemgetter
today = '02-03-2022'
today = date(*map(int, itemgetter(2, 0, 1)(today.split('-'))))
diff = 6 - today.weekday()
Sunday = today + timedelta(days = diff)
print(Sunday)
Solution 2:[2]
Looks like this answer gets you pretty close, just have to switch the integers for the weekday names.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris Charley |
| Solution 2 | Dijon |
