0

I am trying to add extra month in my expiry_date field in Django, I've done like this

import datetime

now = datetime.datetime.now()

year = now.year
day = now.day
hour = now.hour
minute = now.minute
second = now.second

def add_expiry(a):
    month = now.month
    print('month : ', month)
    current_month = month+a
    print('month adding variable : ', current_month)
    date = f"{year}/{current_month}/{day}"
    time = f"{hour}:{minute}:{second}"
    return f"{date} {time}"
print(add_expiry(6))

Output :

month :  6
month adding variable :  12
2021/12/27 11:54:17

but problem is it will increase only month but what about year how I can handle year please help.

Example : current_date = 2021-06-27 12:24:52.976751 if I add 4 as parameter it should return 2021-10-27 12:24:52.976751

and if I add 7 as parameter it should return 2022-04-27 12:24:52.976751

Note : I don't want to use dateutil

Ankit Tiwari
  • 4,438
  • 4
  • 14
  • 41

3 Answers3

1

The easiest way to do this is to use the module dateutil:

>>> from dateutil import relativedelta
>>> datetime.datetime.now() + relativedelta.relativedelta(months=1)
datetime.datetime(2021, 7, 27, 14, 30, 53, 111845)
>>> datetime.datetime.now() + relativedelta.relativedelta(months=4)
datetime.datetime(2021, 10, 27, 14, 32, 20, 238002)
BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • Hello @BoarGules, thanks for your response but it's a package and I don't want to install a package for a single function I think it should done by datetime – Ankit Tiwari Jun 27 '21 at 12:33
  • I said it was the easiest way. Which it is. Working with dates is tricky at the best of times so you might have to do some work to get it right. `datetime.timedelta` can do what you want, but it has no concept of months, or years, only days and seconds, so you will in any case end up doing much of the work that `dateutil` will do correctly for you. – BoarGules Jun 27 '21 at 12:36
  • Hello @BoarGules I think this is the only and easy way to do it. – Ankit Tiwari Jun 27 '21 at 17:40
0

You can do something like this, You have to calculate which 6 months are you picking and the number of days in it,

from datetime import datetime, timedelta

date = datetime.now().date()
months = timedelta(days=30+31+30+31)

# It will equivalent to some 4 months.
print(date + months)
starboy_jb
  • 899
  • 1
  • 6
  • 13
  • Hello @starboy_jb your answer is close to my problem but it's not reusable, the value which is passed in my function it will be dynamic. – Ankit Tiwari Jun 27 '21 at 12:28
  • Refer this link, It may help https://stackoverflow.com/questions/4130922/how-to-increment-datetime-by-custom-months-in-python-without-using-library – starboy_jb Jun 27 '21 at 13:31
0

You can do this:

from datetime import datetime
from dateutil.relativedelta import relativedelta

seven_months = datetime.now() + relativedelta(months=+7)

print(datetime.today())
print(seven_months)  

I think this should work too:

from datetime import datetime


def add_month(date: datetime, month: int) -> datetime:
    added_month = month + date.month
    if added_month > 12:
        new_month = added_month % 12
    else:
        new_month = added_month
    year = 0
    while(added_month > 12):
        year += 1
        added_month -= 12
    new_date = datetime(date.year+year, new_month, date.day)
    return new_date
Mojtaba Arezoomand
  • 2,140
  • 8
  • 23