How do I display 5 years and 5 days later to from my current time Example :
Year : 2017
newYear: 2022
How to do it? My current time format looks like this :
import datetime
X=datetime.datetime.now()
print ("year:%s" %x.year)
It's simplest to use the 3rd party dateutil and relativedelta here which conveniently takes years as a delta option and will handle leap years for you, eg:
from dateutil.relativedelta import relativedelta
dt = datetime.now() + relativedelta(years=5, days=5)
# datetime.datetime(2022, 9, 4, 13, 49, 33, 650299)
Or you can use arrow:
>>> import arrow
>>> ar = arrow.utcnow()
>>> ar.shift(years=5, days=5)
<Arrow [2022-09-04T12:50:26.609842+00:00]>
arrow.utcnow().shift(years=5, days=5) directly...This perhaps:
from calendar import isleap
from datetime import datetime, timedelta
X=datetime.now()
day_count = sum(365 + isleap(yr) for yr in range(X.year + 1, X.year + 6)) + 5
Y = X + timedelta(days=day_count)
Note: timedelta does not accepts years directly, you have to do it using days. It is not the best method but can be done this way.
day_count = sum(365 + isleap(yr) for yr in range(X.year + 1, X.year + 6)) + 5Sorry for my Late reply, I have been extremely busy these past few days. My code will first of all add 5 years to the current year, then add five days, making sure to change it if It goes over the maximum allowed. (Which is 31 for August) But you can expand it for the other months too. This is just the concept.
import datetime
X=datetime.datetime.now()
print ("year:%s" %X.year)
newY = X.year + 5
print("new year: %s" %newY)
newD = X.day + 5
if X.month == 1 or X.month == 3 or X.month == 5 or X.month == 7 or X.month == 8 or X.month == 10 or X.month == 11 or X.month == 12:
# Test if the Month has 31 days
if X.day + 5 > 31:
op1 = X.day + 5
op2 = op1 - 31
new = X.month + 1
print("month: {}".format(new))
newXD = None
timedelta(years=5, days=5)...class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)from the docs docs.python.org/3/library/datetime.html#datetime.timedelta. there seems to be no years argument. I think you're wrong.datetime.now().year + 5…?!