<?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: Calculate age in field calc using python parser in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/calculate-age-in-field-calc-using-python-parser/m-p/442394#M14847</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Using the code box in field calculator, you can subtract two python datatime objects from each other.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For the code block use something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def dateDiff(date): &amp;nbsp;&amp;nbsp; from datetime import datetime &amp;nbsp;&amp;nbsp; return int((datetime.now( ) - datetime.strptime(date,"%m/%d/%Y")).days/365.2425)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In the expression just enter in the name the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;dateDiff( !DOB! )&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Where field DOB is a date field.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 19 Dec 2012 15:43:52 GMT</pubDate>
    <dc:creator>AndrewChapkowski</dc:creator>
    <dc:date>2012-12-19T15:43:52Z</dc:date>
    <item>
      <title>Calculate age in field calc using python parser</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/calculate-age-in-field-calc-using-python-parser/m-p/442393#M14846</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am looking to calculate an Age field from a DOB field using the python Parser in Desktop field calculator. Does anyone know the syntax I would need to use?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Dec 2012 15:48:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/calculate-age-in-field-calc-using-python-parser/m-p/442393#M14846</guid>
      <dc:creator>JustinNettleton</dc:creator>
      <dc:date>2012-12-17T15:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age in field calc using python parser</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/calculate-age-in-field-calc-using-python-parser/m-p/442394#M14847</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Using the code box in field calculator, you can subtract two python datatime objects from each other.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For the code block use something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def dateDiff(date): &amp;nbsp;&amp;nbsp; from datetime import datetime &amp;nbsp;&amp;nbsp; return int((datetime.now( ) - datetime.strptime(date,"%m/%d/%Y")).days/365.2425)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In the expression just enter in the name the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;dateDiff( !DOB! )&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Where field DOB is a date field.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Dec 2012 15:43:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/calculate-age-in-field-calc-using-python-parser/m-p/442394#M14847</guid>
      <dc:creator>AndrewChapkowski</dc:creator>
      <dc:date>2012-12-19T15:43:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age in field calc using python parser</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/calculate-age-in-field-calc-using-python-parser/m-p/442395#M14848</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks, works perfect.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 21 Dec 2012 15:07:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/calculate-age-in-field-calc-using-python-parser/m-p/442395#M14848</guid>
      <dc:creator>JustinNettleton</dc:creator>
      <dc:date>2012-12-21T15:07:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age in field calc using python parser</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/calculate-age-in-field-calc-using-python-parser/m-p/442396#M14849</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This works very well, unless there is a NULL value in a record. Naturally, one doesn't always have a DOB for each record.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I Add Field "Age", then Calculate Field using the above python algorythm... however, I get an error when it hits NULL values.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000539: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Error running expression datediff ( " " ) &amp;lt;type 'exceptions.ValueError'&amp;gt;: time data ' ' does not match format '%Y%m%d'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (Calculate Field: AGE)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Mar 2013 21:07:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/calculate-age-in-field-calc-using-python-parser/m-p/442396#M14849</guid>
      <dc:creator>DanielO_Donnell</dc:creator>
      <dc:date>2013-03-12T21:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age in field calc using python parser</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/calculate-age-in-field-calc-using-python-parser/m-p/442397#M14850</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you are at 10.1, you can just test to see if a value is Null by checking to see if it equals None. This is highlighted in &lt;/SPAN&gt;&lt;A href="http://support.esri.com/en/knowledgebase/techarticles/detail/40913" rel="nofollow noopener noreferrer" target="_blank"&gt;KB 40913&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You should be able to do something like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def dateDiff(date):
&amp;nbsp;&amp;nbsp; from datetime import datetime
&amp;nbsp;&amp;nbsp; if date is None:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return int((datetime.now( ) - datetime.strptime(date,"%m/%d/%Y")).days/365.2425)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:47:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/calculate-age-in-field-calc-using-python-parser/m-p/442397#M14850</guid>
      <dc:creator>LucasDanzinger</dc:creator>
      <dc:date>2021-12-11T19:47:19Z</dc:date>
    </item>
  </channel>
</rss>

