How can we add years to the current date?

317
2
03-28-2023 07:34 AM
SoleneMas
New Contributor II

Good morning,
I am working on arcmap and I would like to set up a script that would allow me to add years.
I have a data arbres_ev which includes as field: date_diag (format_date), periodicity (integer) ,maj_previsional(format_date). the objective is to fill in the field maj_previsional whose value would correspond to: maj_previsional + periodicity therefore if periodicity=1 add one year...
I tested this script:
fields = ['date_diag','periodicity','maj_previsional']
#print(fields)
today = datetime.datetime(today.year, today.month, today.day)
with arcpy.da.UpdateCursor(inFeatures, fields) as cursor:
for the cursor line:
#print(str(row[1])
if ((str(row[0]) != ') and (str(row[1]) =='1'):
row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=365)
if ((str(row[0]) != ') and (str(row[1]) =='2'):
row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=730)
if ((str(row[0]) != ') and (str(row[1]) =='3'):
row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=1095)
if ((str(row[0]) != ') and (str(row[1]) ='4'):
row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=1460)
if ((str(row[0]) != ') and (str(row[1]) =='5'):
row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=1825)
cursor.updateRow(line)
print("Second part complete!")
So for a year it works but as soon as I spend the two years it doesn’t work.
can I use this function? If not, what function should I take?
thanking you for your help

0 Kudos
2 Replies
by Anonymous User
Not applicable

 

What error are you seeing?  Simplifying your code for testing, the days seem to work ok:

dte = datetime.datetime.now()

row1 = dte + datetime.timedelta(days=365)
row2 = dte + datetime.timedelta(days=730)
row3 = dte + datetime.timedelta(days=1095)
row4 = dte + datetime.timedelta(days=1460)
row5 = dte + datetime.timedelta(days=1825)

print(row1)
print(row2)
print(row3)
print(row4)
print(row5)

2024-03-27 08:51:39.793168
2025-03-27 08:51:39.793168
2026-03-27 08:51:39.793168
2027-03-27 08:51:39.793168
2028-03-26 08:51:39.793168

 

It would help of you formatted your code by clicking the ..., then </> to paste and format as python.

0 Kudos
BlakeTerhune
MVP Regular Contributor

Your example code has a lot of syntax errors. If I'm interpreting it correctly, then something like this should work.

 

import arcpy
import datetime

fields = ['date_diag','periodicity','maj_previsional']
expression = "date_diag <> '' and date_diag is not null"
with arcpy.da.UpdateCursor(inFeatures, fields, expression) as cursor:
    for date_diag, periodicity, maj_previsional in cursor:
        if date_diag:
            maj_previsional = maj_previsional + datetime.timedelta(days = periodicity * 365)
            cursor.updateRow([date_diag, periodicity, maj_previsional])

 

If you need to account for leap year/day, then you'll have to update the date calculation.

0 Kudos