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
#1
I am trying to add years in integer form in date to get final output as date. Could anyone help with syntax.
Reply
#2
show us what have you tried, sample input and expected output

probably https://docs.python.org/3/library/dateti...te.replace or https://docs.python.org/3/library/dateti...me.replace
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

Reply
#3
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?
Reply
#4
(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 = datetime.datetime.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

try:
    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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Thanks everyone, I was able to solve this error by firt installing datedelta and then used datedelat.datedelta(years=fieldname())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 123 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 313 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Create dual folder on different path/drive based on the date agmoraojr 2 378 Jan-21-2024, 10:02 AM
Last Post: snippsat
  Python date format changes to date & time 1418 4 518 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 961 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,162 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  PDF properties doesn't show created or modified date Pedroski55 4 1,007 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  How should I run pip-date in python3? newbieAuggie2019 5 1,775 Mar-31-2023, 03:21 PM
Last Post: snippsat
  How to see the date of installation of python modules. newbieAuggie2019 4 1,466 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019
  havent programmed in years - confused by why RETURN is not returning stmoose 5 1,128 Mar-26-2023, 05:51 AM
Last Post: buran

Forum Jump:

User Panel Messages

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