Bleroux

Calculate Field Date

Discussion created by Bleroux on Oct 15, 2012
Latest reply on Oct 15, 2012 by Bleroux
I am creating a script that downloads a file from the previous day and add the contents to a temp gdb. Then I am trying to add a field for the Date the file is from. I can't seem to calculate the date correctly when running CalculateField. I have tried numerous ways resulting in error, incorrect dates, or just the time showing. The code below give me an incorrect date. leaving off the str before the requestDate gives me an Object: Error in Executing tool. Any ideas?

requestDate = date.today() - timedelta(days=1) #Current day - 1 day
importpath = "C:\\PythonScripts\\ProcessLiveHail\\Work\\"
workGDB = importpath + 'WorkDB.gdb'

try:
    if os.path.exists(workGDB):
        arcpy.Delete_management(workGDB)
    arcpy.CreateFileGDB_management(importpath, "WorkDB.gdb")
    arcpy.CopyFeatures_management(importpath + 'hailsize_' + requestDate.strftime("%Y%m%d") + '.shp', workTable)
    arcpy.AddField_management(workTable, 'Date', "DATE")
    arcpy.CalculateField_management(workTable, 'Date', str(requestDate), "PYTHON")
except Exception, e:
    # If an error occurred, print line number and error message
    import traceback, sys
    print e.message

Outcomes