I'm working with Arcpy in ArcGIS and trying to write code that will update a field with the last day of the previous month (ie. if I run the code today, 2017-09-01, it will update the field with 2017-08-31).
If I do a manual field calculation, I can get it to work using VBScript and the pre-logic script code:
Dim FC
FC = DateSerial(Year(Date), Month(Date), 0)
and then setting the field to equal FC:
I've tried to integrate this into code so it can be set to run automatically, but haven't had any luck. Is anyone able to identify where I'm going wrong with the below code?
import arcpy
inTable = r'C:\1 Block Watch Cr\Base_Data.gdb\Districts'
field = "Final_Date"
exp = 'getdate()'
codeblock = '''def getdate():
DateSerial(Year(Date ()), Month(Date ()), 0)'''
arcpy.CalculateField_management(inTable, field, exp, "VB", codeblock)
I am getting the following error message: