1

I have two dates let's say 04-2012 and 05-2014

I need to find a way to get all the months and years between the two dates.

For example:

04-2012
05-2012
...
04-2014
05-2014

I tried to loop through the range but I cloudn't stop at the last month.

Jason
  • 25
  • 5

5 Answers5

0

One way of doint this is as follows:

from datetime import datetime, timedelta 

d1 = '04-2012'
d2 = '05-2014'

t1 = datetime.strptime(d1, "%m-%Y")
t2 = datetime.strptime(d2, "%m-%Y")

delta = timedelta(days=20)

out_dates = []

while t1 <= t2:

    date_str = t1.strftime('%m-%Y')

    if date_str not in out_dates:
        out_dates.append(date_str)

    t1 += delta


print(out_dates)
% gives: ['04-2012', '05-2012', '06-2012', '07-2012', '08-2012', '09-2012', '10-2012', '11-2012', '12-2012', '01-2013', '02-2013', '03-2013', '04-2013', '05-2013', '06-2013', '07-2013', '08-2013', '09-2013', '10-2013', '11-2013', '12-2013', '01-2014', '02-2014', '03-2014', '04-2014', '05-2014']

It will print by days, but you can make later filter it only to months and years.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • @frostnational There are no months in [timedelta](https://docs.python.org/2/library/datetime.html#timedelta-objects). So you can iterate by days and later leave out only months, if thats the case – Marcin Jun 09 '14 at 05:13
  • days=1 is not logical. Also it print the month-year 30 times for each month. – Jason Jun 09 '14 at 05:15
  • even if you tried days=30, you will get a duplicated month for each 4 years. – Jason Jun 09 '14 at 05:16
  • @Jason I modified example. – Marcin Jun 09 '14 at 05:25
0

Assuming you have your dates formatted correctly, you can use timedelta and a simple helper method. https://docs.python.org/2/library/datetime.html#datetime.timedelta

from datetime import timedelta

def dates_in_between(date1, date2):
  date_count = date1
  while(date1 < date2):

    date_count = date + timedelta(months = 1)
    print date_count
Orane
  • 2,223
  • 1
  • 20
  • 33
0

Maybe this will help you to solve your problem:

first_month, first_year = map(int, "04-2012".split("-"))
last_month, last_year = map(int, "05-2014".split("-"))
year = first_year
while year <= last_year:
    print year
    month = first_month if year == first_year else 1
    end_month = last_month if year == last_year else 12
    while month <= end_month:
        print "%0.2d-%d" % (month, year)
        month += 1
    year += 1
dustin
  • 1
  • 1
0

It's tempting to reach for datetime, etc. but you can also solve it with a simple loop:

for year in range(2012, 2015):                                                  
    for month in range(1, 13):                                                  
        if (year, month) >= (2012, 4) and (year, month) <= (2014, 5):           
            print "%02d-%s" % (month, year)
trendels
  • 4,747
  • 3
  • 26
  • 16
0

let dateutil be your friend:

import datetime
from dateutil.rrule import rrule, MONTHLY

d1 = datetime.datetime(2014,2,1)
d2 = datetime.datetime(2015,1,1)

print [d for d in rrule(MONTHLY, dtstart=d1, until=d2)]
Bjoern Stiel
  • 3,918
  • 1
  • 21
  • 19