Select to view content in your preferred language

Calculate Field Date

2304
1
10-15-2012 11:23 AM
BrianLeroux
Frequent Contributor
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

Tags (2)
0 Kudos
1 Reply
BrianLeroux
Frequent Contributor
arcpy.CalculateField_management(workTable, 'Date', "datetime.datetime.today()- datetime.timedelta(days=1)", "PYTHON")


This works. I just thought i would be able to use my requestDate variable as it is performing the exact same function.
0 Kudos