<?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: Using InsertCursor method with DATE type field. in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/using-insertcursor-method-with-date-type-field/m-p/536936#M17834</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;An interesting problem that is glossed over in the help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I solved this by creating a table with a date field, populating it and then reading them with a SearchCursor.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When Python reads/writes a date field using a cursor it expects a date object using the datetime module. So I found that you have to create and manipulate dates as datetime objects.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The datetime module is actually very convenient and cursors work for everything except NULL values. &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# datetest

import arcpy,datetime

ws = "c:/home/python/datetest.gdb"
arcpy.env.workspace = ws
rows = arcpy.UpdateCursor("fcdate")
for row in rows :
&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.timestamp,type(row.timestamp)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.timestamp == None:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.timestamp = datetime.datetime.now()
&amp;nbsp;&amp;nbsp;&amp;nbsp; else :
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.timestamp = row.timestamp + datetime.timedelta(hours=10,minutes=30,seconds=15)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
del row,rows&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example featureclass with an existing date field. It had a few dates and a null record.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Note that you can read the dates and print them because a date object has a default 'representation'. It is &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;not&lt;/SPAN&gt;&lt;SPAN&gt; a string, you can just print the object as a string without casting.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Note how you increment a date, using a timedelta object. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You also have to trap null dates.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have used a file geodatabase (not a shapefile) because I expected it to handle a full datetime not just a date as in dBase or INFO.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;More help in the pythonwin help for python for the datetime module.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 23:18:46 GMT</pubDate>
    <dc:creator>KimOllivier</dc:creator>
    <dc:date>2021-12-11T23:18:46Z</dc:date>
    <item>
      <title>Using InsertCursor method with DATE type field.</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-insertcursor-method-with-date-type-field/m-p/536935#M17833</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How can I use the InsertCursor method with DATE type field? I'm trying to use this code, but have an error (progresscheck - table in my gdb, date - field with date data type):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rows = gp.InsertCursor(progresscheck)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;row = rows.NewRow()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;row.date = "22.10.2010"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;or&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rows = gp.InsertCursor(progresscheck)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;row = rows.NewRow()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;row.date = time.strftime("%d.%m.%Y",time.localtime(time.time()))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Error message: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;File "C:\work\progress\1\1.py", line 61, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;row.date = time.strftime("%d.%m.%Y",time.localtime(time.time()))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;RuntimeError: ERROR 999999: Error executing function.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 04 Jul 2010 19:16:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-insertcursor-method-with-date-type-field/m-p/536935#M17833</guid>
      <dc:creator>BorisBorisov</dc:creator>
      <dc:date>2010-07-04T19:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: Using InsertCursor method with DATE type field.</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-insertcursor-method-with-date-type-field/m-p/536936#M17834</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;An interesting problem that is glossed over in the help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I solved this by creating a table with a date field, populating it and then reading them with a SearchCursor.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When Python reads/writes a date field using a cursor it expects a date object using the datetime module. So I found that you have to create and manipulate dates as datetime objects.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The datetime module is actually very convenient and cursors work for everything except NULL values. &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# datetest

import arcpy,datetime

ws = "c:/home/python/datetest.gdb"
arcpy.env.workspace = ws
rows = arcpy.UpdateCursor("fcdate")
for row in rows :
&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.timestamp,type(row.timestamp)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.timestamp == None:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.timestamp = datetime.datetime.now()
&amp;nbsp;&amp;nbsp;&amp;nbsp; else :
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.timestamp = row.timestamp + datetime.timedelta(hours=10,minutes=30,seconds=15)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
del row,rows&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example featureclass with an existing date field. It had a few dates and a null record.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Note that you can read the dates and print them because a date object has a default 'representation'. It is &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;not&lt;/SPAN&gt;&lt;SPAN&gt; a string, you can just print the object as a string without casting.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Note how you increment a date, using a timedelta object. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You also have to trap null dates.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have used a file geodatabase (not a shapefile) because I expected it to handle a full datetime not just a date as in dBase or INFO.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;More help in the pythonwin help for python for the datetime module.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:18:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-insertcursor-method-with-date-type-field/m-p/536936#M17834</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2021-12-11T23:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using InsertCursor method with DATE type field.</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-insertcursor-method-with-date-type-field/m-p/536937#M17835</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How hard could it be??&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Unfortunately this example script is read-only at 9.3. You will have to upgrade to arcpy to insert dates as I have found to my cost.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1/08/2010 &amp;lt;type 'unicode'&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; exec codeObject in __main__.__dict__&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\eudora\Attachdir\datetest93.py", line 16, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.timestamp = row.timestamp + datetime.timedelta(hours=10,minutes=30,seconds=15)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;TypeError: coercing to Unicode: need string or buffer, datetime.timedelta found&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# datetest93.py
# convert to 9.3 after a problem
import arcgisscripting,datetime

gp = arcgisscripting.create(9.3)
ws = "d:/workspace/datetest.gdb"
gp.workspace = ws
rows = gp.UpdateCursor("fcdate")
row = rows.next()
while row :
&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.timestamp,type(row.timestamp)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.timestamp == None:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.timestamp = datetime.datetime.now() ## fails 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&amp;nbsp;&amp;nbsp;&amp;nbsp; else :
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.timestamp = row.timestamp + datetime.timedelta(hours=10,minutes=30,seconds=15) ## fails
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.next()
del row,rows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:18:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-insertcursor-method-with-date-type-field/m-p/536937#M17835</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2021-12-11T23:18:49Z</dc:date>
    </item>
  </channel>
</rss>

