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

gabor gabor at nekomancer.net
Wed Apr 12 07:42:15 EDT 2006


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



More information about the Python-list mailing list