How to get first/last day of the previous month?

Marco Mariani marco at sferacarta.com
Tue Jan 20 10:06:13 EST 2009


Hussein B wrote:

> I'm creating a report that is supposed to harvest the data for the
> previous month.
> So I need a way to get the first day and the last day of the previous
> month.
> Would you please tell me how to do this?
> Thanks in advance.

dateutil can do this and much, much more.


 >>> from datetime import date
 >>> from dateutil.relativedelta import relativedelta
 >>> today = date.today()
 >>> d = today - relativedelta(months=1)
 >>> date(d.year, d.month, 1)
datetime.date(2008, 12, 1)
 >>> date(today.year, today.month, 1) - relativedelta(days=1)
datetime.date(2008, 12, 31)
 >>>




More information about the Python-list mailing list