Hi all. I am trying to write a script in Python that will populate a date field in a featureclass with a date of my choosing. Not today's date. Can anybody point me in the right direction?
First you need to create an expression value for the field calculation:
exp = 'datetime.datetime(2019,1,17).strftime("%Y/%m/%d")'
Then do the field calculation:
You should be familiar with Python's datetime module and ArcGIS' Update Cursor tool. For a more specific answer, you should describe your work environment. What version of Python and ArcGIS are you using? What is the type of geodatabase?
Hi Randy! Thanks very much for your answer!
I am using PyCharm 2018.3.3 and ArcGIS Pro 10.5 with a File GDB
My current attempt is:
import arcpyimport datetime
arcpy.env.workspace = r"xxxx.gdb"
arcpy.env.overwriteOutput = True
datefrom = datetime.datetime(2019,01,17)dateto = datetime.datetime(2020,01,01)
A_OP_LISTBUILDINGS = arcpy.SelectLayerByLocation_management('LISTED_BUILDINGS', 'INTERSECT', 'FEATURE', 0, 'NEW_SELECTION')
A_OP_LISTBUILDINGS = arcpy.CalculateField_management(A_OP_LISTBUILDINGS,"DateFrom",datefrom)A_OP_LISTBUILDINGS = arcpy.CalculateField_management(A_OP_LISTBUILDINGS,"DateTo",dateto)
However this populates the field with "00:00:00" i.e assuming it's a time. I think this is because it is in the format yyyy,mm,dd but that is the only format datetime.datetime accepts (I think).
This was what i was missing!! Thanks so much! You legend
Retrieving data ...