Python last 60 end-of-months
Question: This is my first post here so please let me know if I'm doing this wrong...

I'm looking to take the last day of each month for the last 60 months, from a
reference date.

For example, if the reference date is today (Aug 21 2014), then the last end
of month is July 31st, 2014, and the one before that is June 30th, 2014...

Does anyone know how to do this in python?

Thanks!


Answer: Here's an easier/cleaner way that makes use of the
[`datetime`](https://docs.python.org/2/library/datetime.html) module:

    
    
    >>> import datetime
    >>> def prevMonthEnd(startDate):
    ...    ''' given a datetime object, return another datetime object that
    ...        is set to the last day of the prevoius month '''
    ...    firstOfMonth = startDate.replace(day=1)
    ...    return firstOfMonth - datetime.timedelta(days=1)
    ...
    >>> theDate = datetime.date.today()
    >>> for i in range(60):
    ...    theDate = prevMonthEnd(theDate)
    ...    print theDate
    ...
    2014-07-31
    2014-06-30
    2014-05-31
    2014-04-30
    2014-03-31
    2014-02-28
    2014-01-31
    2013-12-31
    (etc.)
    

