<?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: How to add a date field and custom date? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216925#M65703</link>
    <description>&lt;P&gt;Sorry about that! I forgot to include the definition here: I have updated the post to include PatrolDateCalc = datetime.date(2022, 9, 19).&lt;/P&gt;</description>
    <pubDate>Wed, 28 Sep 2022 14:12:40 GMT</pubDate>
    <dc:creator>nsidaniel</dc:creator>
    <dc:date>2022-09-28T14:12:40Z</dc:date>
    <item>
      <title>How to add a date field and custom date?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216562#M65688</link>
      <description>&lt;P&gt;I'm trying to add a date field without it taking too long. Using arcpy.da.UpdateCursor took &lt;STRONG&gt;forever&lt;/STRONG&gt;, so I'm trying Add field and calculate field ... which I cannot seem to get working! Any advice is appreciated! ArcGIS 2.9. Below is part of my code.&lt;/P&gt;&lt;P&gt;Thank you - Daniel&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os, sys
import datetime

PatrolDateCalc = datetime.date(2022, 9, 19)
fc = oh
field = "PatrolDate"

if arcpy.ListFields(fc, field) == True:
    Print ("PatrolDate" + fc + "field already present")   
else:
    arcpy.management.AddField(fc, field, "DATE", "10", "", "", "Patrol Date", "", "NON_REQUIRED")
    arcpy.management.CalculateField(fc, field, PatrolDateCalc, "PYTHON3", "", "DATE", "NO_ENFORCE_DOMAINS")

fc = ug
field = "PatrolDate"
if arcpy.ListFields(fc, field) == True:
    Print ("PatrolDate" + fc + "field already present")   
else:
    arcpy.management.AddField(fc, field, "DATE", "10", "", "", "Patrol Date", "", "NON_REQUIRED")
    arcpy.management.CalculateField(fc, field, PatrolDateCalc, "PYTHON3", "", "DATE", "NO_ENFORCE_DOMAINS")


In  [14]:
Line:    arcpy.management.CalculateField(fc, field, PatrolDateCalc, "PYTHON3", "", "DATE", "NO_ENFORCE_DOMAINS")

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in CalculateField:
Line 5588:  raise e

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in CalculateField:
Line 5585:  retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block, field_type, enforce_domains), True)))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in &amp;lt;lambda&amp;gt;:
Line 512:   return lambda *args: val(*gp_fixargs(args, True))

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a SQL Expression.
Failed to execute (CalculateField).&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2022 13:55:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216562#M65688</guid>
      <dc:creator>nsidaniel</dc:creator>
      <dc:date>2022-09-28T13:55:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a date field and custom date?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216598#M65689</link>
      <description>&lt;P&gt;Where is "PatrolDateCalc" declared? I don't see it.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Sep 2022 16:45:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216598#M65689</guid>
      <dc:creator>dgiersz_cuyahoga</dc:creator>
      <dc:date>2022-09-27T16:45:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a date field and custom date?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216599#M65690</link>
      <description>&lt;P&gt;I assume it is complaining about 'PatrolDateCalc', which I done see being defined anywhere.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Sep 2022 16:46:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216599#M65690</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2022-09-27T16:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a date field and custom date?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216925#M65703</link>
      <description>&lt;P&gt;Sorry about that! I forgot to include the definition here: I have updated the post to include PatrolDateCalc = datetime.date(2022, 9, 19).&lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2022 14:12:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216925#M65703</guid>
      <dc:creator>nsidaniel</dc:creator>
      <dc:date>2022-09-28T14:12:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a date field and custom date?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216927#M65704</link>
      <description>&lt;P&gt;Thank you for responding! Yep, PatrolDateCalc is defined in my code, but I forgot to put it here. I've updated the post to include PatrolDateCalc = datetime.date(2022, 9, 19).&lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2022 14:13:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216927#M65704</guid>
      <dc:creator>nsidaniel</dc:creator>
      <dc:date>2022-09-28T14:13:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a date field and custom date?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216945#M65705</link>
      <description>&lt;P&gt;Try using&amp;nbsp;&lt;SPAN&gt;PatrolDateCalc = datetime.datetime(2022, 9, 19)&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2022 14:40:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1216945#M65705</guid>
      <dc:creator>dgiersz_cuyahoga</dc:creator>
      <dc:date>2022-09-28T14:40:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a date field and custom date?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1219386#M65751</link>
      <description>&lt;P&gt;I don't often use the arcpy version of CalculateField, but I have three thoughts and ideas:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Perhaps the "expression" parameter needs to be a string because I think another Python within arcpy has to eval() it.&amp;nbsp; Perhaps the expression parameter literally needs to be "datetime.date(2022, 9, 19)" instead of referencing a variable.&lt;/LI&gt;&lt;LI&gt;If I am correct about the expression parameter, and you still want to use a variable as the date, perhaps you could pass in "datetime.date({0}, {1}, {2})".format(PatrolDateCalc.year, PatrolDateCalc.month, PatrolDateCalc.day) instead&lt;/LI&gt;&lt;LI&gt;Is it a problem having a new field called DATE?&amp;nbsp; Sometimes this is a keyword in SQL databases&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Let us know and good luck!&lt;/P&gt;</description>
      <pubDate>Thu, 06 Oct 2022 15:23:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1219386#M65751</guid>
      <dc:creator>RogerDunnGIS</dc:creator>
      <dc:date>2022-10-06T15:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a date field and custom date?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1219424#M65758</link>
      <description>&lt;P&gt;Here is what worked for me on a file geodatabase on a field declared with the Date type:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.management.CalculateField(myLayer, myField, "'{0}'".format(PatrolDateCalc.strftime("%m/%d/%Y")))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that you may want (or even need) to change the parameter to strftime to match your locale better.&amp;nbsp; I don't know.&lt;/P&gt;&lt;P&gt;Let us know and good luck!&lt;/P&gt;</description>
      <pubDate>Thu, 06 Oct 2022 16:11:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-add-a-date-field-and-custom-date/m-p/1219424#M65758</guid>
      <dc:creator>RogerDunnGIS</dc:creator>
      <dc:date>2022-10-06T16:11:32Z</dc:date>
    </item>
  </channel>
</rss>

