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

M.-A. Lemburg mal at egenix.com
Wed Jan 21 09:56:08 CET 2009


On 2009-01-20 15:54, Mike Driscoll wrote:
> On Jan 20, 8:19 am, Hussein B <hubaghd... at gmail.com> wrote:
>> Hey,
>> 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.
> 
> I recommend the dateutil module:
> 
> http://labix.org/python-dateutil
> 
> I think it may also be possible to use the datetime module, but it's
> easier to just use dateutil.

Or use mxDateTime on which all this was based:

http://www.egenix.com/products/python/mxBase/mxDateTime/

>>> from mx.DateTime import now, Date, RelativeDateTime
>>>
>>> def prev_month():
...     t = now()
...     return (
...         t + RelativeDateTime(months=-1, day=1, hour=0, minute=0, second=0),
...         t + RelativeDateTime(months=-1, day=-1, hour=0, minute=0, second=0))
...
>>> print prev_month()
(<mx.DateTime.DateTime object for '2008-12-01 00:00:00.00' at 2b13fe0e7978>,
<mx.DateTime.DateTime object for '2008-12-31 00:00:00.00' at 2b13fe0e79d0>)


-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jan 21 2009)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/



More information about the Python-list mailing list