<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How can we add years to the current date? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-can-we-add-years-to-the-current-date/m-p/1272476#M67259</link>
    <description>&lt;P&gt;Good morning,&lt;BR /&gt;I am working on arcmap and I would like to set up a script that would allow me to add years.&lt;BR /&gt;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...&lt;BR /&gt;I tested this script:&lt;BR /&gt;fields = ['date_diag','periodicity','maj_previsional']&lt;BR /&gt;#print(fields)&lt;BR /&gt;today = datetime.datetime(today.year, today.month, today.day)&lt;BR /&gt;with arcpy.da.UpdateCursor(inFeatures, fields) as cursor:&lt;BR /&gt;for the cursor line:&lt;BR /&gt;#print(str(row[1])&lt;BR /&gt;if ((str(row[0]) != ') and (str(row[1]) =='1'):&lt;BR /&gt;row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=365)&lt;BR /&gt;if ((str(row[0]) != ') and (str(row[1]) =='2'):&lt;BR /&gt;row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=730)&lt;BR /&gt;if ((str(row[0]) != ') and (str(row[1]) =='3'):&lt;BR /&gt;row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=1095)&lt;BR /&gt;if ((str(row[0]) != ') and (str(row[1]) ='4'):&lt;BR /&gt;row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=1460)&lt;BR /&gt;if ((str(row[0]) != ') and (str(row[1]) =='5'):&lt;BR /&gt;row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=1825)&lt;BR /&gt;cursor.updateRow(line)&lt;BR /&gt;print("Second part complete!")&lt;BR /&gt;So for a year it works but as soon as I spend the two years it doesn’t work.&lt;BR /&gt;can I use this function? If not, what function should I take?&lt;BR /&gt;thanking you for your help&lt;/P&gt;</description>
    <pubDate>Tue, 28 Mar 2023 14:34:02 GMT</pubDate>
    <dc:creator>SoleneMas</dc:creator>
    <dc:date>2023-03-28T14:34:02Z</dc:date>
    <item>
      <title>How can we add years to the current date?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-we-add-years-to-the-current-date/m-p/1272476#M67259</link>
      <description>&lt;P&gt;Good morning,&lt;BR /&gt;I am working on arcmap and I would like to set up a script that would allow me to add years.&lt;BR /&gt;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...&lt;BR /&gt;I tested this script:&lt;BR /&gt;fields = ['date_diag','periodicity','maj_previsional']&lt;BR /&gt;#print(fields)&lt;BR /&gt;today = datetime.datetime(today.year, today.month, today.day)&lt;BR /&gt;with arcpy.da.UpdateCursor(inFeatures, fields) as cursor:&lt;BR /&gt;for the cursor line:&lt;BR /&gt;#print(str(row[1])&lt;BR /&gt;if ((str(row[0]) != ') and (str(row[1]) =='1'):&lt;BR /&gt;row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=365)&lt;BR /&gt;if ((str(row[0]) != ') and (str(row[1]) =='2'):&lt;BR /&gt;row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=730)&lt;BR /&gt;if ((str(row[0]) != ') and (str(row[1]) =='3'):&lt;BR /&gt;row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=1095)&lt;BR /&gt;if ((str(row[0]) != ') and (str(row[1]) ='4'):&lt;BR /&gt;row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=1460)&lt;BR /&gt;if ((str(row[0]) != ') and (str(row[1]) =='5'):&lt;BR /&gt;row[2] = arcpy.time.ParseDateTimeString(str(row[0])) + datetime.timedelta(days=1825)&lt;BR /&gt;cursor.updateRow(line)&lt;BR /&gt;print("Second part complete!")&lt;BR /&gt;So for a year it works but as soon as I spend the two years it doesn’t work.&lt;BR /&gt;can I use this function? If not, what function should I take?&lt;BR /&gt;thanking you for your help&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2023 14:34:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-we-add-years-to-the-current-date/m-p/1272476#M67259</guid>
      <dc:creator>SoleneMas</dc:creator>
      <dc:date>2023-03-28T14:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: How can we add years to the current date?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-we-add-years-to-the-current-date/m-p/1272487#M67260</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What error are you seeing?&amp;nbsp; Simplifying your code for testing, the days seem to work ok:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would help of you formatted your code by clicking the ..., then &amp;lt;/&amp;gt; to paste and format as python.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2023 14:54:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-we-add-years-to-the-current-date/m-p/1272487#M67260</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-03-28T14:54:15Z</dc:date>
    </item>
    <item>
      <title>Re: How can we add years to the current date?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-we-add-years-to-the-current-date/m-p/1272512#M67262</link>
      <description>&lt;P&gt;Your example code has a lot of syntax errors. If I'm interpreting it correctly, then something like this should work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import datetime

fields = ['date_diag','periodicity','maj_previsional']
expression = "date_diag &amp;lt;&amp;gt; '' 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])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you need to account for leap year/day, then you'll have to &lt;A href="https://stackoverflow.com/a/55153593" target="_self"&gt;update the date calculation&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2023 15:17:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-we-add-years-to-the-current-date/m-p/1272512#M67262</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-03-28T15:17:10Z</dc:date>
    </item>
  </channel>
</rss>

