Finding the date of the next Saturday
Solution 1
>>> from datetime import datetime, timedelta
>>> d = datetime.strptime('2013-05-27', '%Y-%m-%d') # Monday
>>> t = timedelta((12 - d.weekday()) % 7)
>>> d + t
datetime.datetime(2013, 6, 1, 0, 0)
>>> (d + t).strftime('%Y-%m-%d')
'2013-06-01'
I use (12 - d.weekday()) % 7
to compute the delta in days between given day and next Saturday because weekday
is between 0 (Monday) and 6 (Sunday), so Saturday is 5. But:
- 5 and 12 are the same modulo 7 (yes, we have 7 days in a week :-) )
- so
12 - d.weekday()
is between 6 and 12 where5 - d.weekday()
would be between 5 and -1 - so this allows me not to handle the negative case (-1 for Sunday).
Here is a very simple version (no check) for any weekday:
>>> def get_next_weekday(startdate, weekday):
"""
@startdate: given date, in format '2013-05-25'
@weekday: week day as a integer, between 0 (Monday) to 6 (Sunday)
"""
d = datetime.strptime(startdate, '%Y-%m-%d')
t = timedelta((7 + weekday - d.weekday()) % 7)
return (d + t).strftime('%Y-%m-%d')
>>> get_next_weekday('2013-05-27', 5) # 5 = Saturday
'2013-06-01'
Solution 2
I found this pendulum pretty useful. Just one line
In [4]: pendulum.now().next(pendulum.SATURDAY).strftime('%Y-%m-%d')
Out[4]: '2019-04-27'
See below for more details:
In [1]: import pendulum
In [2]: pendulum.now()
Out[2]: DateTime(2019, 4, 24, 17, 28, 13, 776007, tzinfo=Timezone('America/Los_Angeles'))
In [3]: pendulum.now().next(pendulum.SATURDAY)
Out[3]: DateTime(2019, 4, 27, 0, 0, 0, tzinfo=Timezone('America/Los_Angeles'))
In [4]: pendulum.now().next(pendulum.SATURDAY).strftime('%Y-%m-%d')
Out[4]: '2019-04-27'
Solution 3
You need two main packages,
import datetime
import calendar
Once you have those, you can simply get the desired date for the week by the following code,
today = datetime.date.today() #reference point.
saturday = today + datetime.timedelta((calendar.SATURDAY-today.weekday()) % 7 )
saturday
Bonus Following the content, if you type
saturday.weekday()
It will result 5
.
Therefore, you can also use 5
in place of calendar.SATURDAY
and you will get same result.
saturday = today + datetime.timedelta((5-today.weekday()) % 7 )
Solution 4
if you just want the date from today (inspired from Emanuelle )
def get_next_weekday(weekday_number):
"""
@weekday: week day as a integer, between 0 (Monday) to 6 (Sunday)
"""
assert 0 <= weekday_number <= 6
today_date = datetime.today()
next_week_day = timedelta((7 + weekday_number - today_date.weekday()) % 7)
return (today_date + next_week_day).strftime('%d/%m/%Y')
akkatracker
Student in Sydney, Australia Languages: Python HTML/CSS Javascript (less than I'd like to) Ruby (a bit) PHP (the smallest amount of) English ;)
Updated on July 27, 2022Comments
-
akkatracker almost 2 years
How would one go about finding the date of the next Saturday in Python? Preferably using
datetime
and in the format '2013-05-25'? -
Mattwmaster58 over 2 yearsThere's no need to add the modulo base (7) to avoid negative numbers. "The modulo operator always yields a result with the same sign as its second operand."
-
Emmanuel over 2 years@Mattwmaster58: didn't know, thanks!