<?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: Field Calculator in Python using Dates in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/field-calculator-in-python-using-dates/m-p/130496#M10140</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I don't know off the top of my head how to do it in Python, but in VBScript you should be able to use the example on &lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Calculating_an_attribute_to_the_number_of_days_between_two_dates/005s0000001r000000/"&gt;this help page&lt;/A&gt; (and divide by 7).&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I finally got it to work, after realizing that when you use !Date! in the field calculator, it actually returns a string rather than a datetime.date() object.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In my pre-processor I had the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[PHP]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;firstTime = datetime.date(2009, 7, 16)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;def timeDif(a):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; month, date, year = a.split('/')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; featDate = datetime.date(int(year), int(month), int(date))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; timeDiff = featDate - firstTime&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return timeDiff.days / 7&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[/PHP]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and in my new field I simply had:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[PHP]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;timeDif(!Date!)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[/PHP]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Frustrating to figure it out, but hey, it works now.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 03 Feb 2012 21:12:48 GMT</pubDate>
    <dc:creator>MichalisAvraam</dc:creator>
    <dc:date>2012-02-03T21:12:48Z</dc:date>
    <item>
      <title>Field Calculator in Python using Dates</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-in-python-using-dates/m-p/130494#M10138</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a field in my shapefile that is of type "Date".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to figure out the difference between that "Date" and today, in weeks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My assumption was that (datetime.date() - !Date!) would produce a timedelta object, which i would simply call to find out the difference. Hence my code below:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;(datetime.date.today() - !Date!).days / 7&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Of course, that says it is a syntax error, which doesn't explain much. Any pointers to manipulating dates in the field calculator?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Feb 2012 20:54:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-in-python-using-dates/m-p/130494#M10138</guid>
      <dc:creator>MichalisAvraam</dc:creator>
      <dc:date>2012-02-03T20:54:04Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator in Python using Dates</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-in-python-using-dates/m-p/130495#M10139</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I don't know off the top of my head how to do it in Python, but in VBScript you should be able to use the example on &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Calculating_an_attribute_to_the_number_of_days_between_two_dates/005s0000001r000000/"&gt;this help page&lt;/A&gt;&lt;SPAN&gt; (and divide by 7).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Feb 2012 21:07:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-in-python-using-dates/m-p/130495#M10139</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2012-02-03T21:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator in Python using Dates</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-in-python-using-dates/m-p/130496#M10140</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I don't know off the top of my head how to do it in Python, but in VBScript you should be able to use the example on &lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Calculating_an_attribute_to_the_number_of_days_between_two_dates/005s0000001r000000/"&gt;this help page&lt;/A&gt; (and divide by 7).&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I finally got it to work, after realizing that when you use !Date! in the field calculator, it actually returns a string rather than a datetime.date() object.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In my pre-processor I had the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[PHP]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;firstTime = datetime.date(2009, 7, 16)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;def timeDif(a):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; month, date, year = a.split('/')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; featDate = datetime.date(int(year), int(month), int(date))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; timeDiff = featDate - firstTime&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return timeDiff.days / 7&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[/PHP]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and in my new field I simply had:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[PHP]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;timeDif(!Date!)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[/PHP]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Frustrating to figure it out, but hey, it works now.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Feb 2012 21:12:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-in-python-using-dates/m-p/130496#M10140</guid>
      <dc:creator>MichalisAvraam</dc:creator>
      <dc:date>2012-02-03T21:12:48Z</dc:date>
    </item>
  </channel>
</rss>

