0

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

1 Answer 1

6

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.