43๐
โ
With dateutil:
>>> import datetime
>>> d1 = datetime.date.today()
>>> from dateutil.relativedelta import relativedelta
>>> d1 + relativedelta(months=1)
datetime.date(2012, 4, 8)
>>> d2 = datetime.date(year=2012,month=1,day=31)
>>> d2 + relativedelta(months=1)
datetime.date(2012, 2, 29)
๐คdani herrera
2๐
As Niklas commented, since months vary in length, one month from today can be pretty ambiguous.
Every industry has some sort of convention; the result may be different depending on your goals. For example, will it be used for interest calculations? will it be used to generate recurrent bills?
If you want 30 days from today:
>>> import datetime
>>> d1 = datetime.date.today()
>>> d1
datetime.date(2012, 3, 8)
>>> d1 + datetime.timedelta(30)
datetime.date(2012, 4, 7)
May not be what you want if month has 31 days:
>>> d2 = datetime.date(2012, 1, 1)
>>> d2 + datetime.timedelta(30)
datetime.date(2012, 1, 31)
>>> import calendar
>>> calendar.monthrange(2012, 1)
(6, 31)
>>> d2 + datetime.timedelta(calendar.monthrange(d2.year, d2.month)[1])
datetime.date(2012, 2, 1)
Yet, may not be the result you expect if next month has less than 30 days:
>>> d3 = datetime.date(2012, 1, 31)
>>> d3 + datetime.timedelta(calendar.monthrange(d3.year, d3.month)[1])
datetime.date(2012, 3, 2)
>>> import dateutil
>>> d3 + dateutil.relativedelta.relativedelta(months=1)
datetime.date(2012, 2, 29)
๐คPaulo Scardine
- Django: Accessing request in forms.py clean function
- Django Models Number Field
- Django filter on APIView
- Trouble getting Django set up on Heroku using South โ keep getting ProgrammingError: relation does not exist
0๐
If you are using the datetime filed, you can pull out the month add it and then set it back.
such as:
d = datetime.date(2003, 7, 29)
d=d.month+1
of course i am still confused on how date time works if your not a delimiter using [โ.โ] would be your best bet
๐คEdgarAllenBro
- AuthAlreadyAssociated Exception in Django Social Auth
- How to avoid putting environment variables into multiple places with Django, nginx and uWSGI?
- Making Twitter, Tastypie, Django, XAuth and iOS work to Build Django-based Access Permissions
- How to do pagination using mongoengine?
Source:stackexchange.com