ArcPy – How to Calculate Day Difference Between Two Fields in ArcGIS Pro

arcgis-proarcpydate

I work with ArcGIS Pro 3.1. I have a GDB table with two date fields. I would like calculate with Python code the difference between two date in days. But the result is not correct if the months are different (For example: ID 3 and 4).

arcpy.management.AddField(tabl, "day","TEXT")
exc= "computeDiff(!e_date!, !s_date!)"
codeblock="""
def computeDiff(start,end):
  FMT = "%Y. %M. %d."
  tday = datetime.datetime.strptime(start, FMT) - datetime.datetime.strptime(end,FMT)
  return str(tday)"""
arcpy.management.CalculateField(tabl, "day", exc, "PYTHON_9.3",codeblock)

enter image description here

Best Answer

The format code for "month" is a lower-case %m. You're currently specifying "year minute day".

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

Related Question