-2

Say I want to find all Mondays between two dates, date_A and date_B

from datetime import date, timedelta
days_to_substract = 50
date_A = date.today()-timedelta(days=days_to_subtract)
date_B = date.today()

How can I do this? Is there any way to iterate between dates?

Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
  • possible duplicate of [Generate a list of datetimes between an interval in python](http://stackoverflow.com/questions/10688006/generate-a-list-of-datetimes-between-an-interval-in-python) – Martijn Pieters Mar 26 '14 at 17:00
  • 1
    Find your first monday, then use the linked post to generate your range, using a `timedelta(days=7)` step. – Martijn Pieters Mar 26 '14 at 17:00
  • With the comment of @MartijnPieters in mind, you can get the nearest coming Monday like this: `dt = date.today(); next_mon = dt + timedelta(days=(7 - dt.weekday()))` – anon582847382 Mar 26 '14 at 17:09

4 Answers4

2

you can iterate over date range with generator:

def date_range(date1,date2):
    while date1 < date2:
        yield date1
        date1 = date1 + timedelta(days=1)

and iterate it like this:

for dat in date_range(date_A,date_B):
    <your code here>
Elisha
  • 4,811
  • 4
  • 30
  • 46
1

Do you mean something like this?

def day_iter(start, end):
    step = timedelta(days=1)
    for i in range((end - start).days):
        yield start + i*step

end = date.today()
start = end - timedelta(days=50)
for day in day_iter(start, end):
    day.weekday() == 0:
        print day.strftime("%A  %Y-%m-%d")

Output:

Monday  2014-02-10
Monday  2014-02-17
Monday  2014-02-24
Monday  2014-03-03
Monday  2014-03-10
Monday  2014-03-17
Monday  2014-03-24

You could also do something like this:

today = date.today()
past = today - timedelta(50)
if past.weekday() != 0:
    monday = past + timedelta(7 - past.weekday())
else:
    monday = past
one_week = timedelta(7)
while monday < today:
    print monday
    monday += one_week

which outputs:

2014-02-10
2014-02-17
2014-02-24
2014-03-03
2014-03-10
2014-03-17
2014-03-24

And you can generalize this into a function like this:

def past_weekdays(depth, weekday):
    today = date.today()
    past = today - timedelta(depth)
    if past.weekday() != weekday:
        past_day = past + timedelta((7 + (weekday - past.weekday())) % 7)
    else:
        past_day = past
    one_week = timedelta(7)
    while past_day < today:
        yield past_day
        past_day += one_week
Broseph
  • 1,655
  • 1
  • 18
  • 38
1

Find last Monday from current date and find remaining based on 7 days difference:

from datetime import date, timedelta
days_to_substract = 50
date_A = date.today()-timedelta(days=days_to_substract)
date_B = date.today()
cDay = date_B - timedelta(days=date_B.weekday())
print cDay
while cDay > date_A:
    cDay = cDay - timedelta(days=7)
    print cDay

Output:

2014-03-24
2014-03-17
2014-03-10
2014-03-03
2014-02-24
2014-02-17
2014-02-10
2014-02-03
venpa
  • 4,268
  • 21
  • 23
1

Maybe this :

from datetime import date, timedelta

days_to_substract = 50
date_A = date.today()-timedelta(days=days_to_substract)
date_B = date.today()

date_tmp = date_A

numOfMondays = 0
deltaStep = 1

while date_tmp < date_B:
    if date_tmp.strftime("%A") != "Monday":
        print("Date before first monday {}".format(date_tmp))
        date_tmp += timedelta(days=1)
    else:
        print("Monday : {}".format(date_tmp))
        date_tmp += timedelta(days=7)
        numOfMondays += 1

print("from {} to {} there are {} monday".format(date_A, date_B, numOfMondays))

with output

Date before first monday 2014-02-05
Date before first monday 2014-02-06
Date before first monday 2014-02-07
Date before first monday 2014-02-08
Date before first monday 2014-02-09
Monday : 2014-02-10
Monday : 2014-02-17
Monday : 2014-02-24
Monday : 2014-03-03
Monday : 2014-03-10
Monday : 2014-03-17
Monday : 2014-03-24
from 2014-02-04 to 2014-03-26 there are 7 monday
Salvatore Avanzo
  • 2,656
  • 1
  • 21
  • 30