<?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, subtract Expiry date from the Current Date in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/field-calculator-subtract-expiry-date-from-the/m-p/10111#M884</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Try this code with !Expiry! field only:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def NoDays(d1):
&amp;nbsp; c1 = datetime.datetime.strptime(d1.split()[0], "%Y-%m-%d").date()
&amp;nbsp; c2 = datetime.datetime.now().date()
&amp;nbsp; return (c1-c2).days&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 20:25:31 GMT</pubDate>
    <dc:creator>MarcinGasior</dc:creator>
    <dc:date>2021-12-10T20:25:31Z</dc:date>
    <item>
      <title>Field calculator, subtract Expiry date from the Current Date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-subtract-expiry-date-from-the/m-p/10110#M883</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am new to Python and not a programmer. I wish to use the field calculator to subtract an "Expiry Date" from the current date. I have used the following code (which works), but it is using a field containing the current date. How do I change this code so it uses only one field and subtracts from the current date?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Many thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Alan&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import datetime&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;def NoDays(d1, d2):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; c1 = datetime.datetime.strptime (d1.split () [0], "%Y/%m/%d")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; c2 = datetime.datetime.strptime (d2.split () [0], "%Y/%m/%d")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; n = (c1 - c2).days&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; return n&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;NoDays( !Expiry! , !Current!)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Feb 2013 08:19:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-subtract-expiry-date-from-the/m-p/10110#M883</guid>
      <dc:creator>AlanGoldsmith</dc:creator>
      <dc:date>2013-02-12T08:19:52Z</dc:date>
    </item>
    <item>
      <title>Re: Field calculator, subtract Expiry date from the Current Date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-subtract-expiry-date-from-the/m-p/10111#M884</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Try this code with !Expiry! field only:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def NoDays(d1):
&amp;nbsp; c1 = datetime.datetime.strptime(d1.split()[0], "%Y-%m-%d").date()
&amp;nbsp; c2 = datetime.datetime.now().date()
&amp;nbsp; return (c1-c2).days&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:25:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-subtract-expiry-date-from-the/m-p/10111#M884</guid>
      <dc:creator>MarcinGasior</dc:creator>
      <dc:date>2021-12-10T20:25:31Z</dc:date>
    </item>
    <item>
      <title>Re: Field calculator, subtract Expiry date from the Current Date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-subtract-expiry-date-from-the/m-p/10112#M885</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Think you can use either the 'today' or 'now' function of datetime.datetime:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;On my computer, if I do this at IDLE's command line to test returns of both functions they seem to behave similarly:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; testNow = datetime.datetime.now(); testToday = datetime.datetime.today() &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is the result (the same time):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; print testNow&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2013-02-12 06:20:12.998000&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; print testToday&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2013-02-12 06:20:12.998000&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's the thing though - you have 2 lines in your def function that converts the 'fed' time values and converts only the date component into a string that includes backslashes '/' --- &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;c1 = datetime.datetime.strptime (d1.split () [0], "%Y/%m/%d")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;c2 = datetime.datetime.strptime (d2.split () [0], "%Y/%m/%d")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...so 'strptime' needs 2 parameters - '.split()[0]' requires the 'd' values to be string and the 2nd parameter for formatting '%Y/%m/%d' assumes that '/' is in the string, not the '-' that is returned on my system.&amp;nbsp; So you may have to 'coerce' that to work by changing the explicit formatting, something like this may work, you have to test it:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import datetime def NoDays(d1): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c1 = datetime.datetime.strptime (d1.split () [0], "%Y/%m/%d") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; current = datetime.datetime.today() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c2 = datetime.datetime.strptime (str(current).split () [0], "%Y-%m-%d") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n = (c1 - c2).days &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return n&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...and your expression would be the date in the past, such as:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;NoDays( !Expiry! ) &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps - I cannot be sure of the formatting "%Y-%m-%d", so if it fails try switching it back to "%Y/%m/%d" (which should still work for your field values).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Feb 2013 09:50:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-subtract-expiry-date-from-the/m-p/10112#M885</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-02-12T09:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: Field calculator, subtract Expiry date from the Current Date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-subtract-expiry-date-from-the/m-p/10113#M886</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you very much to Marcin and Wayne, it was Wayne's code that solved the issue.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'll be back!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2013 10:09:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-subtract-expiry-date-from-the/m-p/10113#M886</guid>
      <dc:creator>AlanGoldsmith</dc:creator>
      <dc:date>2013-02-25T10:09:15Z</dc:date>
    </item>
  </channel>
</rss>

