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)
8
  • You can adapt this answer to use timedelta(years=5, days=5) ... Commented Aug 30, 2017 at 12:39
  • @JonClements 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. Commented Aug 30, 2017 at 12:44
  • @s_vishnu ooops - confusing that with dateutil's relativedelta... Commented Aug 30, 2017 at 12:45
  • Possible duplicate of Add one year in current date PYTHON Commented Aug 30, 2017 at 12:52
  • You're talking about days, but all you show is years… so datetime.now().year + 5…?! Commented Aug 30, 2017 at 12:56

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

Comments

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]>

1 Comment

Always forget about arrow and it's quite nice that you can do arrow.utcnow().shift(years=5, days=5) directly...
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.

5 Comments

Never heard of leap years ?
You might also want to check your logic... This gives you 2022-07-22...
If you're going to do it this way - make it way less complicated (and correct) and use day_count = sum(365 + isleap(yr) for yr in range(X.year + 1, X.year + 6)) + 5
@Eduardo you're welcome - you only need the +5 in one place though.
@JonClements Thanks, I overlooked it
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.