Is there an easy way to increase a datetime object per month in Python?

So, I'm trying to find a way to increase the datetime object by one month. However, it seems that it is not so easy, according to this question .

I was hoping for something like:

import datetime as dt now = dt.datetime.now() later = now + dt.timedelta(months=1) 

But that does not work. I also hoped that I could go the same day (or the closest alternative) next month, if possible. For example, the datetime object set on January 1 will increase until February 1, while the datetime object set on February 28 will increase until March 31, and not March 28 or something else.

To make it clear that February 28 (usually) will be displayed until March 31, because it is the last day of the month, and therefore it must go on the last day of the month for the next month. Otherwise, it will be a direct link: the increment should go to the day next month with the same numbered day.

Is there an easy way to do this in the current version of Python?

+6
source share
4 answers

Check out from dateutil.relativedelta import * to add a certain amount of time to the date you can continue to use timedelta for simple things timedelta

 use_date = use_date + datetime.timedelta(minutes=+10) use_date = use_date + datetime.timedelta(hours=+1) use_date = use_date + datetime.timedelta(days=+1) use_date = use_date + datetime.timedelta(weeks=+1) 

or you can start using relativedelta

 use_date = use_date+relativedelta(months=+1) use_date = use_date+relativedelta(years=+1) 

on the last day of the following month:

 use_date = use_date+relativedelta(months=+1) use_date = use_date+relativedelta(day=31) 

Now it will provide 02/29/2016

on the penultimate day of the following month:

 use_date = use_date+relativedelta(months=+1) use_date = use_date+relativedelta(day=31) use_date = use_date+relativedelta(days=-1) 

last Friday of next month:

 use_date = use_date+relativedelta(months=+1, day=31, weekday=FR(-1)) 

Second Tuesday of next month:

 new_date = use_date+relativedelta(months=+1, day=1, weekday=TU(2)) 

This is by no means an exhaustive list of what is available. Documentation is available here: https://dateutil.readthedocs.org/en/latest/

+8
source

How about this?

 def add_one_month(orig_date): # advance year and month by one month new_year = orig_date.year new_month = orig_date.month + 1 # note: in datetime.date, months go from 1 to 12 if new_month > 12: new_year += 1 new_month -= 12 new_day = orig_date.day # while day is out of range for month, reduce by one while True: try: new_date = datetime.date(new_year, new_month, new_day) except ValueError as e: new_day -= 1 else: break return new_date 

EDIT:

An improved version that:

  • saves time information if a datetime.datetime object is specified
  • doesn't use try / catch, instead using calendar.monthrange from the calendar module in stdlib:
 import datetime import calendar def add_one_month(orig_date): # advance year and month by one month new_year = orig_date.year new_month = orig_date.month + 1 # note: in datetime.date, months go from 1 to 12 if new_month > 12: new_year += 1 new_month -= 12 last_day_of_month = calendar.monthrange(new_year, new_month)[1] new_day = min(orig_date.day, last_day_of_month) return orig_date.replace(year=new_year, month=new_month, day=new_day) 
+1
source

Question: Is there an easy way to do this in the current version of Python?

Answer. There is no easy (direct) way in the current version of Python.

Link: see docs.python.org/2/library/datetime.html , section 8.1.2. timedelta Objects. As we can understand from this, we cannot increase the month directly, since this is not a single unit of time.

A plus. If you want the first day β†’ the first day and the last day β†’ matching the last day, you must handle this separately for different months.

0
source
 >>> now datetime.datetime(2016, 1, 28, 18, 26, 12, 980861) >>> later = now.replace(month=now.month+1) >>> later datetime.datetime(2016, 2, 28, 18, 26, 12, 980861) 

EDIT: not working

 y = datetime.date(2016, 1, 31); y.replace(month=2) results in ValueError: day is out of range for month 

Ther is not an easy way to do this, but you can use your own function as shown below.

-1
source

All Articles