2

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)
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • You can adapt [this answer](https://stackoverflow.com/questions/13685201/how-to-add-hours-to-current-time-in-python/13685221#13685221) to use `timedelta(years=5, days=5)` ... – Jon Clements Aug 30 '17 at 12:39
  • @JonClements `class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)` from the docs https://docs.python.org/3/library/datetime.html#datetime.timedelta. there seems to be no years argument. I think you're wrong. – void Aug 30 '17 at 12:44
  • @s_vishnu ooops - confusing that with dateutil's relativedelta... – Jon Clements Aug 30 '17 at 12:45
  • Possible duplicate of [Add one year in current date PYTHON](https://stackoverflow.com/questions/15741618/add-one-year-in-current-date-python) – yinnonsanders Aug 30 '17 at 12:52
  • You're talking about days, but all you show is years… so `datetime.now().year + 5`…?! – deceze Aug 30 '17 at 12:56
  • @deceze `datetime(2016, 2, 29).replace(year=2021)`... – Jon Clements Aug 30 '17 at 12:59
  • @Jon The point is about if OP is only interested in the year itself. – deceze Aug 30 '17 at 13:00
  • @deceze sure... simple in that case.. but the *and 5 days later* not so much. – Jon Clements Aug 30 '17 at 13:01

4 Answers4

4

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)
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
3

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]>
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
0

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.

Eduardo
  • 631
  • 1
  • 12
  • 25
0

Sorry 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