0

I have the following code which works on a increament of month. However, after feburary it shows last day as 28 for each month. How can I get the actual last day of each month while increasing each month.

from dateutil.relativedelta import relativedelta
import datetime
from datetime import date
import time

Start_Date = date(2010, 01, 01)
End_Date = date(2010, 01, 31)
Final_startdate = date(2011, 12, 01)

while Start_Date < Final_startdate:
    print Start_Date
    print End_Date

    Start_Date = Start_Date + relativedelta(months=+1)
    End_Date = End_Date + relativedelta(months=+1)

This is what I get:

2010-01-01

2010-01-31

2010-02-01

2010-02-28

2010-03-01

2010-03-28

2010-04-01

2010-04-28

2010-05-01

2010-05-28

2010-06-01

2010-06-28

2010-07-01

2010-07-28

2010-08-01

2010-08-28

A.S
  • 305
  • 1
  • 4
  • 20
  • @glibdud : Thank you for referring to the duplicate question. I got my answer there. – A.S Nov 30 '17 at 19:27

1 Answers1

0

I changed last line of code for end_date to the one below and it works. I got my desired answer.

End_Date = Start_Date + relativedelta(months=+1,days=-1)

The answer I got is

2010-01-01

2010-01-31

2010-02-01

2010-02-28

2010-03-01

2010-03-31

2010-04-01

2010-04-30

2010-05-01

2010-05-31

2010-06-01

2010-06-30

2010-07-01

2010-07-31

2010-08-01

A.S
  • 305
  • 1
  • 4
  • 20