<?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 Re: Automating date calculations in field calculator in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068378#M61370</link>
    <description>&lt;P&gt;Thank you for your help, that has worked. I just need to create an 'if' statement now.&lt;/P&gt;&lt;P&gt;Is one able to loop the statement in the field calculator whenever a new entry is created?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 15 Jun 2021 14:30:52 GMT</pubDate>
    <dc:creator>DarrenDe_Lange</dc:creator>
    <dc:date>2021-06-15T14:30:52Z</dc:date>
    <item>
      <title>Automating date calculations in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068363#M61368</link>
      <description>&lt;P&gt;Good afternoon all!&lt;/P&gt;&lt;P&gt;I am trying to automate date calculations using the field calculator in ArcGIS Pro.&lt;/P&gt;&lt;P&gt;I have defined survey dates, which I would like to use to create a reinspection dates.&lt;/P&gt;&lt;P&gt;The reinspection date is 1 year for high risk zones, 3 years for medium risk zone and 5 years for low risk zones.&lt;/P&gt;&lt;P&gt;Is there a piece of code I could use in the field calculator to automate this calculation?&lt;/P&gt;&lt;P&gt;The best sample I came out with for high risk zones is:&lt;/P&gt;&lt;P&gt;Expression:&lt;BR /&gt;arcpy.time.ParseDateTimeString(!SurveyDate!) + datetime.timedelta(days=365)&lt;/P&gt;&lt;P&gt;However, this did not work when I tried to execute the script.&lt;/P&gt;&lt;P&gt;Any help is appreciated.&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 13:49:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068363#M61368</guid>
      <dc:creator>DarrenDe_Lange</dc:creator>
      <dc:date>2021-06-15T13:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: Automating date calculations in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068372#M61369</link>
      <description>&lt;P&gt;Regarding&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;However, this did not work when I tried to execute the script.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;It always helps to be more specific.&amp;nbsp; What exactly does "did not work" mean?&amp;nbsp; Did you get an error?&amp;nbsp; If so, what error, specifically?&amp;nbsp; Did you get an unexpected result?&amp;nbsp; if so, what were you expecting and what did you get?&lt;/P&gt;&lt;P&gt;You should be able to simply retrieve the date and add the time to it:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;!SurveyDate! + datetime.timedelta(days=365)&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 15 Jun 2021 14:09:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068372#M61369</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-06-15T14:09:00Z</dc:date>
    </item>
    <item>
      <title>Re: Automating date calculations in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068378#M61370</link>
      <description>&lt;P&gt;Thank you for your help, that has worked. I just need to create an 'if' statement now.&lt;/P&gt;&lt;P&gt;Is one able to loop the statement in the field calculator whenever a new entry is created?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 14:30:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068378#M61370</guid>
      <dc:creator>DarrenDe_Lange</dc:creator>
      <dc:date>2021-06-15T14:30:52Z</dc:date>
    </item>
    <item>
      <title>Re: Automating date calculations in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068380#M61371</link>
      <description>&lt;P&gt;You say it did not work but what was the result? No values? Incorrect values? Error message? In the case of an error, what was the complete message (check the geoprocessing results)?&lt;/P&gt;&lt;P&gt;If you're working with the datetime library, you'll need to &lt;FONT face="courier new,courier"&gt;import datetime&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Try something like this for the Python code block&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def calc_reinspection(survey_date):
    date_str_format = "%d/%m/%Y"
    survey_date = datetime.datetime.strptime(survey_date, date_str_format)
    reinspection_date = survey_date + datetime.timedelta(days=365)
    return datetime.datetime.strftime(reinspection_date, date_str_format)&lt;/LI-CODE&gt;&lt;P&gt;That last line is formatting the datetime object back to a string formatted date to exclude the time portion (which defaults to midnight). You can leave that part out if you don't mind your dates all having the time displayed (even though they do have it hidden); just &lt;FONT face="courier new,courier"&gt;return reinspection_date&lt;/FONT&gt;.&lt;/P&gt;&lt;P&gt;And this for the Python expression:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;calc_reinspection(!SurveyDate!)&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 15 Jun 2021 14:32:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068380#M61371</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-06-15T14:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: Automating date calculations in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068518#M61373</link>
      <description>&lt;P&gt;Not sure what you mean by looping the statement in the field calculator. Calculate field will perform the operation on all (or selected) records once. If you want it to do multiple things when you run calculate field, you'll have to code that logic; possibly using an if statement.&lt;/P&gt;&lt;P&gt;If you want to automatically do something when a condition is met (new record, new value, etc), that would be something different. Maybe look into &lt;A href="https://solutions.arcgis.com/shared/help/attribute-assistant/" target="_self"&gt;Attribute Assistant&lt;/A&gt;&amp;nbsp;or &lt;A href="https://pro.arcgis.com/en/pro-app/latest/help/data/geodatabases/overview/an-overview-of-attribute-rules.htm" target="_self"&gt;Attribute Rules&lt;/A&gt;. If it's in an RDBMS, you could make a database trigger. Or if it doesn't need to be immediate, you could &lt;A href="https://www.esri.com/arcgis-blog/products/arcgis-pro/analytics/schedule-a-python-script-or-model-to-run-at-a-prescribed-time-2019-update/" target="_self"&gt;schedule a Python script&lt;/A&gt; as a Windows task.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 18:07:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068518#M61373</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-06-15T18:07:57Z</dc:date>
    </item>
    <item>
      <title>Re: Automating date calculations in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068782#M61375</link>
      <description>&lt;P&gt;Hi Blake - Thank you for your response.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am now trying to add in the risk zones and my statement does not seem to be working.&lt;/P&gt;&lt;P&gt;Field Calculator:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Expression line:
reclass(!SurveyDate!)

Code block:
def reclass(SurveyDate):
if (RiskZone = LOW):
return "SurveyDate + datetime.timedelta(days=486)"
elif (RiskZone = MED):
return return "SurveyDate + datetime.timedelta(days=1095)"
elif (RiskZone = HIGH):
return return "SurveyDate + datetime.timedelta(days=1825)"&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;Attached is the syntax error.&lt;/P&gt;&lt;P&gt;Any assistance is appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jun 2021 08:09:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068782#M61375</guid>
      <dc:creator>DarrenDe_Lange</dc:creator>
      <dc:date>2021-06-16T08:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: Automating date calculations in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068960#M61377</link>
      <description>&lt;P&gt;That error is because you are using a single = which is for assignment. Use double == for comparing equality. And you probably want quotes around the LOW value to indicate it's a string and not a variable that isn't defined.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;if (RiskZone == "LOW"):&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, get rid of the double return keywords; just one will do&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jun 2021 16:18:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1068960#M61377</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-06-16T16:18:57Z</dc:date>
    </item>
    <item>
      <title>Re: Automating date calculations in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1069243#M61380</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jun 2021 07:37:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automating-date-calculations-in-field-calculator/m-p/1069243#M61380</guid>
      <dc:creator>DarrenDe_Lange</dc:creator>
      <dc:date>2021-06-17T07:37:02Z</dc:date>
    </item>
  </channel>
</rss>

