datetime: the date of the day one month ago...how?
Christos Georgiou
tzot at sil-tec.gr
Wed Apr 12 16:46:40 EDT 2006
On Wed, 12 Apr 2006 13:42:15 +0200, rumours say that gabor
<gabor at 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
More information about the Python-list
mailing list