[Tutor] Subract Month From Date

Tim Peters tim.peters at gmail.com
Tue May 3 20:16:34 CEST 2005


[Gooch, John]
> Thank you for the idea, I could have been more clear that days part of the
> date isn't important. Here is what I came up with:
>
>    currentDate = datetime.datetime.fromtimestamp( time.time() )

Easier:

    today = datetime.date.today()

>    archMonth = 0
>    archYear = 0
>    if ( currentDate.month == 1 ):
>        archMonth = 12
>        archYear = currentYear - 1
>    else:
>        archMonth = currentDate.month -1
>        archYear = currentDate.year
>    archDate = datetime.datetime( archYear, archMonth, 1 )#Year/Month of
> target files

Easier:

    lastmonth = today.replace(day=1) - datetime.timedelta(days=1)

IOW, it moves to the first day of the current month, and then
subtracts one day.  That moves you to the last day of the preceding
month.

    lastmonth.month

is then the month you want, and

    lastmonth.year

is the year you want.


More information about the Tutor mailing list