I'm using ArcGIS 10.3.1 for Desktop to test before writing an answer to Entering date with arcpy.GetParameterAsText()? I have written the test code below that I am running against a test feature class (testFC
) with four line features to try and update its dateField
with a constant date value of "01.07.2015".
import arcpy
dmyString = "01.07.2015"
fc = r"C:\temp\test.gdb\testFC"
item = "dateField"
dmyDate = datetime.datetime.strptime(dmyString,'%d.%m.%Y')
print dmyDate
arcpy.CalculateField_management(fc,item,dmyDate,"PYTHON")
with arcpy.da.SearchCursor(fc,[item]) as cursor:
for row in cursor:
print row[0]
I think the above should work, and from the output below you can see that the date object (dmyDate
) seems to be created fine, but the values written to each of the four features are not what I would expect.
>>> ================================ RESTART ================================
>>>
2015-07-01 00:00:00
1899-12-30 00:00:00
1899-12-30 00:00:00
1899-12-30 00:00:00
1899-12-30 00:00:00
>>>
Can someone explain why 1899-12-30 00:00:00
is being written instead of 2015-07-01 00:00:00
?
The attribute table displays as below:
If I replace the CalculateField line above with either:
arcpy.CalculateField_management(fc,item,"date '2015-07-01 00:00:00'","PYTHON")
or
arcpy.CalculateField_management(fc,item,"date '2015/07/01 00:00:00'","PYTHON")
as per @Vince's comments I receive an error:
Traceback (most recent call last):
File "C:\temp\test.py", line 11, in <module>
arcpy.CalculateField_management(fc,item,"date '2015-07-01 00:00:00'","PYTHON")
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 3457, in CalculateField
raise e
ExecuteError: ERROR 000539: SyntaxError: invalid syntax (<expression>, line 1)
Failed to execute (CalculateField).
expression
parameter. Have you tried using a string value, formatted "date 'YYYY-MM-DD HH:MM:SS'" or "date 'YYYY/MM/DD HH:MM:SS'"?date '2015-07-01'
anddate '2015-07-01 00:00:00'
should be valid with FGDB sources. Having the date formatting fail silently like that is, IMHO, a bug which should be fixed.