9

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.

  • 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 Oct 7 '14 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. – fathom analytics Oct 8 '14 at 11:51
9

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'
  • 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? – fathom analytics Oct 8 '14 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 Oct 8 '14 at 13:29
0

Try using:

time.strftime("%Y/%m/%d")

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • Not sure where to modify the code using this part. Can you edit your post to show the entire code? – fathom analytics Oct 8 '14 at 12:16
  • this line has been incorporated into the answer above – detroit_hc Oct 8 '14 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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