Python Forum
How to add date and years(integer) to get a date
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to add date and years(integer) to get a date
I am trying to add years in integer form in date to get final output as date. Could anyone help with syntax.
show us what have you tried, sample input and expected output

probably or
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

What have you tried? Show us your code and where you are stuck. Have you tried timedelta(days=years*365) but got stuck on leap years?
(Sep-02-2020, 07:22 PM)NG0824 Wrote: I am trying to add years in integer form in date to get final output as date. Could anyone help with syntax.

In [1]: import datetime

In [2]: now =

In [3]: print(now)
2020-09-03 10:01:22.062401

In [4]: # set a year
   ...: past = now.replace(year=1900)

In [5]: print(past)
1900-09-03 10:01:22.062401

In [6]: # adding years to current year
   ...: future = now.replace(now.year + 10) # 10 years in future

In [7]: leap_year = datetime.datetime(2020, 2, 29) # impossible date for non-leap years

In [8]: print(leap_year)
2020-02-29 00:00:00

In [9]: leap_year.replace(year=2019)  # booom
ValueError                                Traceback (most recent call last)
<ipython-input-9-164fec2883be> in <module>
----> 1 leap_year.replace(year=2019)  # booom

ValueError: day is out of range for month
If you wonder, this comes from IPython. A better REPL for Python.
Working with dates is not always straightforward. Sometimes it ends into impossible dates.

To catch this leap_error:

import datetime

one_day = datetime.timedelta(days=1)
leap_year = datetime.datetime(2020, 2, 29)
new_year = 2019

    new_dt = leap_year.replace(year=new_year)
except ValueError:
    new_dt = (leap_year + one_day).replace(year=new_year)
Important stuff to read:
I hope this pushes you in the right direction.
Just keep in mind, that the replace method does not change the object itself, it returns a new modified datetime object.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
Thanks everyone, I was able to solve this error by firt installing datedelta and then used datedelat.datedelta(years=fieldname())

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 328 May-04-2021, 10:51 PM
Last Post: rhat398
  IF statement to apply at each date illmattic 2 765 Apr-08-2021, 12:31 PM
Last Post: illmattic
  String to Date format SAF 2 498 Apr-06-2021, 02:09 PM
Last Post: snippsat
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 506 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Naming the file as time and date. BettyTurnips 3 583 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  Update Date based on Time/String stevezemlicka 1 457 Jan-08-2021, 06:54 PM
Last Post: Gribouillis
  Formatting date in a dataframe WiPi 1 377 Jan-06-2021, 11:26 AM
Last Post: WiPi
  Confusion about [date]time [formatting] Mark17 0 473 Dec-17-2020, 07:25 PM
Last Post: Mark17
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 470 Dec-06-2020, 04:14 PM
Last Post: Divya577
  Convert date integers (ex. 43831) to regular format Galven 2 667 Nov-15-2020, 11:38 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020