I use datetime, i have:
delta = ((datetime.date.today() + datetime.timedelta(days=1)) - datetime.timedelta(???)).isoformat()
I need replace ??? with day to end of the month.
How to do it?
I use datetime, i have:
delta = ((datetime.date.today() + datetime.timedelta(days=1)) - datetime.timedelta(???)).isoformat()
I need replace ??? with day to end of the month.
How to do it?
To compute the end-of-the-current-month date, take the first of the current month, add 1 month and subtract 1 day. For example, for 28 September, you'll end up with
1 September + 1 month = 1 October
1 October - 1 day = 30 September.
import datetime
d = datetime.date(2013,12,8)
last_day = datetime.date(d.year, (d.month+1)%12, 1) - datetime.timedelta(1,0,0)
To properly deal with dates around a new year, I found dateutil.relativedelta to be a better alternative.
See also How to increment datetime month in python.
import datetime
from dateutil.relativedelta import relativedelta
d = datetime.date(2013,12,8)
last_day = datetime.date(d.year, d.month, 1)+relativedelta(months=1, days=-1)