Python Find a day dates between a range of two dates

Viewed 1134

I'm working on a project using python3 in which I have to consume an api and need to fetch the data for a specific Day of the week between a range a date.

So, the example input parameters are:

firstDay = '1-January-2000'
lastDay = '22-february-2000'
weekDay = 'Monday'

So, the date will be included in the API response but nothing has any information for the day. So, I have to get all the dates for a specific day like Monday between a date range.

According to the example above, how can I get the dates for all Mondays between firstDay and lastDay?

3 Answers
from datetime import date, timedelta, datetime
import time

firstDay = '1-January-2000'
lastDay = '22-february-2000'
weekDay = 'Monday'

firstDay = datetime.strptime(firstDay, '%d-%B-%Y') 
lastDay = datetime.strptime(lastDay, '%d-%B-%Y')
dates = [firstDay + timedelta(days=x) for x in range((lastDay-firstDay).days + 1) if (firstDay + timedelta(days=x)).weekday() == time.strptime(weekDay, '%A').tm_wday]

output

dates
[datetime.datetime(2000, 1, 3, 0, 0),
 datetime.datetime(2000, 1, 10, 0, 0),
 datetime.datetime(2000, 1, 17, 0, 0),
 datetime.datetime(2000, 1, 24, 0, 0),
 datetime.datetime(2000, 1, 31, 0, 0),
 datetime.datetime(2000, 2, 7, 0, 0),
 datetime.datetime(2000, 2, 14, 0, 0),
 datetime.datetime(2000, 2, 21, 0, 0)]


The output in format weekDay-Month-year

[d.strftime("%A-%B-%Y") for d in dates] 

['Monday-January-2000',
 'Monday-January-2000',
 'Monday-January-2000',
 'Monday-January-2000',
 'Monday-January-2000',
 'Monday-February-2000',
 'Monday-February-2000',
 'Monday-February-2000']

output in firstDay format: 1-January-2000

[d.strftime("%-d-%B-%Y") for d in dates]   
['3-January-2000',
 '10-January-2000',
 '17-January-2000',
 '24-January-2000',
 '31-January-2000',
 '7-February-2000',
 '14-February-2000',
 '21-February-2000']

please try this:

import datetime
import calendar

def get_all_weekdays_between_range(firstDay, lastDay, weekDay):
    month_name_to_num = {v.lower(): k for k, v in enumerate(calendar.month_name)}
    day_name_to_num = {v.lower(): k for k, v in enumerate(calendar.day_name)}

    def parse_date(dateStr):
        parts = dateStr.split('-')
        return datetime.date(int(parts[2]), month_name_to_num[parts[1].lower()], int(parts[0]))

    first = parse_date(firstDay)
    last = parse_date(lastDay)

    result = []
    current = first
    while current <= last:
        if calendar.weekday(current.year, current.month, current.day) == day_name_to_num[weekDay.lower()]:
            result.append(current)
        current += datetime.timedelta(days=1)
    return result


firstDay = '1-January-2000'
lastDay = '22-february-2000'
weekDay = 'Monday'

all_days = get_all_weekdays_between_range(firstDay,lastDay,weekDay)
for day in all_days:
    print(day)

Have you tried using the datetime library in python, this is a perfect scenario for it. It holds all information about years months and days and can even do calculations of dates. For example finding how many days are between two dates. It can also convert dates into whatever format you want.

https://docs.python.org/3/library/datetime.html

Here's an answer to your question

from datetime import date, timedelta
dstart = datetime.strptime('1-January-2000', '%d-%B-%Y') 
dend= datetime.strptime('22-February-2000', '%d-%B-%Y')
ans = (dend - dstart).days / 7
if int(ans) + 2/7 <= ans:
    ans = int(ans) + 1
else:
    ans = int(ans)
print(ans)
Related