<?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 function in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421355#M33111</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Matthew,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is one option using the field calculator.&amp;nbsp; The below example worked when I had the date in the format: 8/15/2010.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;from datetime import datetime

def calculate_age(born):
&amp;nbsp;&amp;nbsp;&amp;nbsp; today = date.today()
&amp;nbsp;&amp;nbsp;&amp;nbsp; bornDate =datetime.strptime(born, '%m/%d/%Y')
&amp;nbsp;&amp;nbsp;&amp;nbsp; return today.year - bornDate.year - ((today.month, today.day) &amp;lt; (bornDate.month, bornDate.day))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="screen1.PNG" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/129038_screen1.PNG" style="height: auto;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 19:02:09 GMT</pubDate>
    <dc:creator>JakeSkinner</dc:creator>
    <dc:date>2021-12-11T19:02:09Z</dc:date>
    <item>
      <title>Calculate age function</title>
      <link>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421353#M33109</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Does anyone have a python code block that calculates age based on a date field within a table?&amp;nbsp; I have tried a few different things but to no success.&amp;nbsp; Was wondering if someone has successfully done this?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Sep 2015 15:43:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421353#M33109</guid>
      <dc:creator>mpboyle</dc:creator>
      <dc:date>2015-09-30T15:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age function</title>
      <link>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421354#M33110</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Use a timedelta.&amp;nbsp; It should work for seconds, days, weeks, months, years, etc...&amp;nbsp; Easiest example I found is below.&amp;nbsp; Just get your minimum and maximum dates in the field/table and determine the difference.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://stackoverflow.com/questions/1345827/how-do-i-find-the-time-difference-between-two-datetime-objects-in-python" title="http://stackoverflow.com/questions/1345827/how-do-i-find-the-time-difference-between-two-datetime-objects-in-python"&gt;http://stackoverflow.com/questions/1345827/how-do-i-find-the-time-difference-between-two-datetime-objects-in-python&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Sep 2015 15:50:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421354#M33110</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2015-09-30T15:50:43Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age function</title>
      <link>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421355#M33111</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Matthew,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is one option using the field calculator.&amp;nbsp; The below example worked when I had the date in the format: 8/15/2010.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;from datetime import datetime

def calculate_age(born):
&amp;nbsp;&amp;nbsp;&amp;nbsp; today = date.today()
&amp;nbsp;&amp;nbsp;&amp;nbsp; bornDate =datetime.strptime(born, '%m/%d/%Y')
&amp;nbsp;&amp;nbsp;&amp;nbsp; return today.year - bornDate.year - ((today.month, today.day) &amp;lt; (bornDate.month, bornDate.day))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="screen1.PNG" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/129038_screen1.PNG" style="height: auto;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:02:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421355#M33111</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T19:02:09Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age function</title>
      <link>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421356#M33112</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;James,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I try finding the difference between today's date and the date in my field, I get an error: unsupported operand type(s) for: 'datetime.date' and 'unicode'.&amp;nbsp; Is there a way to format the date field to something other than unicode?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Below is the function I have now.&amp;nbsp; The field I'm passing in for 'bday' is a Date field within a file gdb.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import datetime

def calcAge(bday):
&amp;nbsp; today = datetime.date.today()
&amp;nbsp; diff = today - bday
&amp;nbsp; return diff&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:02:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421356#M33112</guid>
      <dc:creator>mpboyle</dc:creator>
      <dc:date>2021-12-11T19:02:12Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age function</title>
      <link>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421357#M33113</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/8586"&gt;Jake Skinner&lt;/A&gt;​,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I try your method, I get an error: 'global name 'date' is not defined'.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Sep 2015 16:17:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421357#M33113</guid>
      <dc:creator>mpboyle</dc:creator>
      <dc:date>2015-09-30T16:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age function</title>
      <link>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421358#M33114</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I know this is in the Python section, but if all you need is number of days, VBScript handles it much easier (below is for shapefile, but should work similarly for GDB):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14436308651956354 jive_text_macro" data-renderedposition="71_8_912_16" jivemacro_uid="_14436308651956354"&gt;&lt;P&gt;Now (&amp;nbsp; ) - [YOURDATE]&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;or years:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14436310782995827 jive_text_macro" data-renderedposition="150_8_912_16" jivemacro_uid="_14436310782995827"&gt;&lt;P&gt;CInt((&lt;SPAN style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;Now (&amp;nbsp; ) - [YOURDATE])/365&lt;/SPAN&gt;)&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Sep 2015 16:35:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421358#M33114</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-09-30T16:35:44Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age function</title>
      <link>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421359#M33115</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try replacing the today variable with 'today = datetime.now( )'.&amp;nbsp; Ex:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;from datetime import datetime

def calculate_age(born):
&amp;nbsp;&amp;nbsp;&amp;nbsp; today = datetime.now( )
&amp;nbsp;&amp;nbsp;&amp;nbsp; bornDate =datetime.strptime(born, '%m/%d/%Y')
&amp;nbsp;&amp;nbsp;&amp;nbsp; return today.year - bornDate.year - ((today.month, today.day) &amp;lt; (bornDate.month, bornDate.day))&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:02:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421359#M33115</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T19:02:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age function</title>
      <link>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421360#M33116</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Welcome to the wonderful world of date manipulation!&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think Jake has your answer below.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Sep 2015 17:31:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421360#M33116</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2015-09-30T17:31:25Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate age function</title>
      <link>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421361#M33117</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks &lt;A href="https://community.esri.com/migrated-users/8586"&gt;Jake Skinner&lt;/A&gt;​!&amp;nbsp; This worked &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Sep 2015 17:42:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-age-function/m-p/421361#M33117</guid>
      <dc:creator>mpboyle</dc:creator>
      <dc:date>2015-09-30T17:42:33Z</dc:date>
    </item>
  </channel>
</rss>

