5

I need to create a day variable which will store the current date, and I need another variable which will store the deadline(day + 1 month), but I seem to be doing something wrong.

import datetime

day = datetime.date.today()
deadline = datetime.date.today()
deadline.month += 1

print(day)
print(deadline)
Petar_Rudovic
  • 51
  • 1
  • 1
  • 2
  • 2
    Does this answer your question? [How do I calculate the date six months from the current date using the datetime Python module?](https://stackoverflow.com/questions/546321/how-do-i-calculate-the-date-six-months-from-the-current-date-using-the-datetime) – Oskar Jan 07 '21 at 23:29
  • How do you want it to react if the next month is short? For example `2021-01-31` plus one month? – Manngo Feb 24 '22 at 00:13

2 Answers2

12

documentation : https://dateutil.readthedocs.io/en/stable/relativedelta.html

import datetime
from dateutil.relativedelta import relativedelta

day = datetime.date.today()
# day --> datetime.date(2021, 1, 8)


deadline = day + relativedelta(months=1)
# deadline --> datetime.date(2021, 2, 8)
Dieter
  • 2,499
  • 1
  • 23
  • 41
0

You could try importing python-dateutil and use relativedelta to achieve what you want. You could refer to this answer for more info

deadline = day + relativedelta(months=1)