470,849 Members | 1,074 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

datetime: the date of the day one month ago...how?

hi,

i'm trying to get the date of the day one month ago.

for example:

today = 12.apr.2006
one-month-ago = 12.mar.2006

so:

one-month-ago(12.apr.2006) = 12.mar.2006

of course sometimes it gets more complicated, like:

one-month-ago(31.mar.2006)

or

one-month-ago(1.jan.2006)

the datetime.timedelta objects only work with hours or days or weeks,
not month (i understand why)...

but is there a way to calculate this in python?

i really don't want to calculate it by myself :-))

thanks,
gabor
Apr 12 '06 #1
4 9790
gabor wrote:
hi,

i'm trying to get the date of the day one month ago.

for example:

today = 12.apr.2006
one-month-ago = 12.mar.2006

so:

one-month-ago(12.apr.2006) = 12.mar.2006

of course sometimes it gets more complicated, like:

one-month-ago(31.mar.2006)

or

one-month-ago(1.jan.2006)

the datetime.timedelta objects only work with hours or days or weeks,
not month (i understand why)...

but is there a way to calculate this in python?

i really don't want to calculate it by myself :-))

It is application specific. So how *do* you want
one-month-ago(31.mar.2006) or one-month-ago(28.feb.2006) to work? No one
can know but you.
--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

Phone: +45 66 11 84 94
Mobile: +45 29 93 42 96
Apr 12 '06 #2
gabor wrote:
hi,

i'm trying to get the date of the day one month ago.

for example:

today = 12.apr.2006
one-month-ago = 12.mar.2006


dateutil has one implementation of this:
http://labix.org/python-dateutil

Kent
Apr 12 '06 #3
Max M wrote:
gabor wrote:
hi,

i'm trying to get the date of the day one month ago.

for example:

today = 12.apr.2006
one-month-ago = 12.mar.2006

so:

one-month-ago(12.apr.2006) = 12.mar.2006

of course sometimes it gets more complicated, like:

one-month-ago(31.mar.2006)

or

one-month-ago(1.jan.2006)

the datetime.timedelta objects only work with hours or days or weeks,
not month (i understand why)...

but is there a way to calculate this in python?

i really don't want to calculate it by myself :-))

It is application specific. So how *do* you want
one-month-ago(31.mar.2006) or one-month-ago(28.feb.2006) to work? No one
can know but you.


well, give me a solution for ANY behaviour :)

or, let's specify it then:

i want the day that you get by intutively saying "one month ago". means
usually picking the same day in the previous month. if that day does not
exist, i want the nearest day that exist and was BEFORE the nonexistent day.

one-month-ago(31.mar.2006) = 28.feb.2006
one-month-ago(28.feb.2006) = 28.jan.2006
so, now that we have a spec, is there a way to achieve this in python
without writing the algorithm by myself?
thanks,
gabor
Apr 12 '06 #4
On Wed, 12 Apr 2006 13:42:15 +0200, rumours say that gabor
<ga***@nekomancer.net> might have written:
i want the day that you get by intutively saying "one month ago". means
usually picking the same day in the previous month. if that day does not
exist, i want the nearest day that exist and was BEFORE the nonexistent day.

one-month-ago(31.mar.2006) = 28.feb.2006
one-month-ago(28.feb.2006) = 28.jan.2006


def submonth(d):
year, month= d.year, d.month
if month == 1:
year-= 1; month= 12
else:
month-= 1
try:
return d.replace(year=year, month=month)
except ValueError:
return d.replace(day=1) - datetime.timedelta(1)

submonth(datetime.date(2006,3,31)) datetime.date(2006, 2, 28) submonth(datetime.date(2006,2,28))

datetime.date(2006, 1, 28)

--
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians
Apr 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Paul Moore | last post: by
6 posts views Thread by Terry Hancock | last post: by
5 posts views Thread by Abdolhosein Vakilzadeh Ebrahimi | last post: by
6 posts views Thread by Ante Perkovic | last post: by
2 posts views Thread by Tim::.. | last post: by
24 posts views Thread by rn5a | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.