Add/subtract week, month, year to current date in python/django

You can add weeks, months and years to current date using dateutil.relativedelta module.

As shown below, using dateutil.relativedelta is straight forward and no need to explain this. You can see examples shown below:

from dateutil.relativedelta import relativedelta

currentDate = datetime.datetime.now()

# add 2 months to currentDate
newDate = currentDate + relativedelta(months=2)

# add 3 months to currentDate
newDate = currentDate + relativedelta(months=3)

# add 1 week to currentDate
newDate = currentDate + relativedelta(weeks=1)

# add 1 year to currentDate
newDate = currentDate + relativedelta(years=1)

Get date: current date + Two days

upcomingDate = datetime.datetime.now().date() + datetime.timedelta(days=2)

You can add/subtract timedelta to current or specific time using timedelta.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top
Shares