python - How to get all the months during a two dates -
i have 2 dates let's 04-2012 , 05-2014
i need find way months , years between 2 dates.
for example:
04-2012 05-2012 ... 04-2014 05-2014
i tried loop through range cloudn't stop @ last month.
one way of doint 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 print days, can make later filter months , years.
Comments
Post a Comment