Python program to get the nth weekday after a given date
Given a date and weekday index, the task is to write a Python program to get the date for the given day of the week occurring after the given date. The weekday index is based on the below table:
Index |
Weekday |
0 |
Monday |
1 |
Tuesday |
2 |
Wednesday |
3 |
Thursday |
4 |
Friday |
5 |
Saturday |
6 |
Sunday |
Examples:
Input : test_date = datetime.datetime(2017, 3, 14), weekday_idx = 4
Output : 2017-03-17
Explanation : 14 March is Tuesday, i.e 1 weekday, 4th weekday is a Friday, i.e 17 March.
Input : test_date = datetime.datetime(2017, 3, 12), weekday_idx = 5
Output : 2017-03-18
Explanation : 12 March is Sunday, i.e 6th weekday, 5th weekday in next week is a Saturday, i.e 18 March.
Method #1 : Using timedelta() + weekday()
In this, we subtract the date weekday from the weekday index and then check for required index extracted, then the required day, if negative is summed with 7 and then the resultant number is added to current date using timedelta().
Python3
import datetime
test_date = datetime.datetime( 2017 , 3 , 14 )
print ( "The original date is : " + str (test_date)[: 10 ])
weekday_idx = 4
days_delta = weekday_idx - test_date.weekday()
if days_delta < = 0 :
days_delta + = 7
res = test_date + datetime.timedelta(days_delta)
print ( "Next date of required weekday : " + str (res)[: 10 ])
|
Output
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
Method #2 : Using lambda function
Using the lambda function provides a shorthand and compact solution to the question.
Python3
import datetime
test_date = datetime.datetime( 2017 , 3 , 14 )
print ( "The original date is : " + str (test_date)[: 10 ])
weekday_idx = 4
def lfnc(test_date, weekday_idx): return test_date + \
datetime.timedelta(days = (weekday_idx - test_date.weekday() + 7 ) % 7 )
res = lfnc(test_date, weekday_idx)
print ( "Next date of required weekday : " + str (res)[: 10 ])
|
Output
The original date is : 2017-03-14
Next date of required weekday : 2017-03-17
Method #3: Using a list comprehension and slicing
In this method, we first generate a list of all dates after the given date up to 7 days later using a list comprehension and the range() function. We add an extra day to the range so that we can slice the list up to 7 days later without including the 8th day.
Next, we use another list comprehension to filter the list to include only dates with the desired weekday.
Finally, we select the nth occurrence of the weekday by indexing the filtered list using [n-1], since the list is zero-indexed but the problem specifies a 1-indexed occurrence.
Python3
from datetime import datetime, timedelta
test_date = datetime( 2017 , 3 , 14 )
weekday_idx = 4
print ( "The original date is:" , str (test_date)[: 10 ])
n = 1
dates = [test_date + timedelta(days = i) for i in range ( 8 )]
filtered_dates = [d for d in dates if d.weekday() = = weekday_idx]
result = filtered_dates[n - 1 ]
print ( "Next date of required weekend is:" ,result.strftime( '%Y-%m-%d' ))
|
Output
The original date is: 2017-03-14
Next date of required weekend is: 2017-03-17
Last Updated :
11 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...