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

H

Hussein B

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.
 
M

Mike Driscoll

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.

Mike
 
D

Diez B. Roggisch

Hussein said:
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?

First day: create a new date-object with the day==1.

Last day: create a date-object with month + 1 and day==1, and subtract a
timedelta with one day of it.

Diez
 
C

Carsten Haese

Hussein said:
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.

In order to not deprive you of the sense of accomplishment from figuring
things out for yourself, I'll give you a couple of hints instead of
fully formed Python code:

1) Think about how you can find the first day of the *current* month.

2) Think about how you can get to the last day of the previous month
from there.

3) Think about how you can get to the first day of the previous month
from there.

Hope this helps,
 
M

Marco Mariani

Hussein said:
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.

 
M

Marco Mariani

Carsten said:
In order to not deprive you of the sense of accomplishment

Sorry for spoiling that. If you still want the sense of accomplishment,
try to reimplement dateutil (and rrule). It's not as easy as it seems :-o
 
C

Carsten Haese

Marco said:
dateutil can do this and much, much more.

Using dateutil for this is like using a sledgehammer to kill a fly. The
task at hand can (and IMHO should) be solved with the standard datetime
module.
 
T

Tim Chase

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?
.... if not when: when = datetime.today()
.... this_first = date(when.year, when.month, 1)
.... prev_end = this_first - timedelta(days=1)
.... prev_first = date(prev_end.year, prev_end.month, 1)
.... return prev_first, prev_end
....(datetime.date(2008, 2, 1), datetime.date(2008, 2, 29))

-tkc
 
M

Marco Mariani

Carsten said:
Using dateutil for this is like using a sledgehammer to kill a fly. The
task at hand can (and IMHO should) be solved with the standard datetime
module.

Sure, but many python programmers are not even aware of the existence of
that particular sledgehammer, it deserved to be mentioned. It should be
part of the standard library I think.
 
H

Hussein B

In order to not deprive you of the sense of accomplishment from figuring
things out for yourself, I'll give you a couple of hints instead of
fully formed Python code:

1) Think about how you can find the first day of the *current* month.

2) Think about how you can get to the last day of the previous month
from there.

3) Think about how you can get to the first day of the previous month
from there.

Hope this helps,

Thanks all for the reply.
Yes, I prefer to use the standard library.
Talking about the third step:
You told me to think how to get the first day of the previous month,
well how to know if the previous month is 28, 29, 30 or 31 days?
I'm new to Python, so forgive my questions.
 
T

Tim Chase

You told me to think how to get the first day of the previous month,
well how to know if the previous month is 28, 29, 30 or 31 days?

Find the first day of the *current* month, and then subtract one
day (use the timedelta object). You'll end up with the last day
of the previous month as a date/datetime object.

-tkc
 
J

John Machin

Sorry for spoiling that. If you still want the sense of accomplishment,
try to reimplement dateutil (and rrule). It's not as easy as it seems :-o

True, but getting the last day of a given month from first principles
without any library routines is nowhere near as complex as (e.g.)
converting (y,m,d) <-> ordinal day number.

It's this easy:

# days in month (non-leap year)
_dim = (None, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

def _leap(y):
if y % 4: return 0
if y % 100: return 1
if y % 400: return 0
return 1

def last_day_of_month(y, m):
"""Return day (1..31) which is last day of month m in year y
"""
if m == 2:
return 28 + _leap(y)
if not (1 <= m <= 12):
raise Exception("month not in 1..12")
return _dim[m]

Cheers,
John
 
M

M.-A. Lemburg

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/
.... 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))
....(<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)________________________________________________________________________

::: 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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top