10

I'm working on part of tool that will use arcpy.CalculateField_management to add the current date to the attribut table. I've wandered far and wide on the interwebs and can't seem to find the resolution to this issue.

When using this code, i get the value "12:00:00 AM"

    input = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
    today = datetime.date.today()
    dte = today.strftime('%m/%d/%Y')
    arcpy.CalculateField_management(input,"DTCARXTRCT",dte,"PYTHON")

When using this code, I get the value "06/19/1905" from today's date of "10/07/2014"

    input = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
    today = datetime.date.today()
    dte = str(today)
    arcpy.CalculateField_management(input,"DTCARXTRCT",dte,"PYTHON")

Any thoughts on what I'm doing wrong? I'd like to avoid using the update cursor, but will do so if it's the last option.

2
  • It appears that the Python portion of this code is working correctly interms of the date. But I think the problem is with the Arcpy portion of the code. Try "VB" instead of "PYTHON" and see if that updates the correct value in the field.
    – F_Kellner
    Commented Oct 7, 2014 at 20:01
  • This didn't resolve the problem, but did have the odd result of changing the value displayed to 12:00:54 AM. Commented Oct 8, 2014 at 11:51

2 Answers 2

12

An update Cursor will out perform the field calculator 100% of the time.

You need to write this as an expression:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
exp = '''def add_date():
  import time
  return time.strftime("%Y/%m/%d")'''

arcpy.CalculateField_management(fc, field, 'add_date()',
                                'PYTHON', exp)
print 'done'

datetime.date.today() did not work in field calculator, switched to strftime.

OR, if you want to do it the better way where you can feed your own variables in, use a cursor:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
with arcpy.da.UpdateCursor(fc, [field]) as rows:
    for row in rows:
        rows.updateRow([datetime.date.today()])
print 'done'
2
  • I tested both of these and they work great. Thanks! On a side note, I don't want to use the update cursor because it requires writing more for something that should be relatively simple. It may be that i'm new to python and don't understand the usefulness of the function. On the face of it using a packaged gp tool makes more sense to me than using the cursor. Thoughts? Commented Oct 8, 2014 at 12:00
  • 2
    That is completely understandable. I too feared the cursors when I was first getting started with Python. However, as you get more comfortable with Python, I think you will find the cursors are much more flexible. Also, as I mentioned in my original answer, the cursors have much better performance. At the county I used to work for, a GIS consultant had written a script for a very long process that used the field calculator many times and this took over 2 hours to run. When I got better with Python, I rewrote the script and used cursors instead of the field calc and it ran in under 15 min.
    – crmackey
    Commented Oct 8, 2014 at 13:29
1

Try using:

time.strftime("%Y/%m/%d")
2
  • Not sure where to modify the code using this part. Can you edit your post to show the entire code? Commented Oct 8, 2014 at 12:16
  • this line has been incorporated into the answer above
    – detroit_hc
    Commented Oct 8, 2014 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.